如何在asp.net中的行数据绑定事件中绑定gridview中的dropdownlist [英] how to bind dropdownlist in gridview in row databound event in asp.net

查看:52
本文介绍了如何在asp.net中的行数据绑定事件中绑定gridview中的dropdownlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在asp.net中的行数据绑定事件中绑定网格视图中的下拉列表?请帮助我

how to bind dropdownlist in gridview in row databound event in asp.net???please help me

推荐答案

检查以下代码



Check this following code

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
            DropDownList ddlDropDownList = (DropDownList)e.Row.FindControl("ddl1");
if (ddlDropDownList != null)
            {
                SqlDataAdapter da = new SqlDataAdapter("select distinct(source) from pickupdroptariff", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    ddlDropDownList.DataSource = dt;
                    ddlDropDownList.DataTextField = "source";
                    ddlDropDownList.DataValueField = "source";
                    ddlDropDownList.DataBind();
                    ddlDropDownList.Items.Insert(0, "--Select--");

                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Source Not Available');", true);
                }
            }
}


试试这个

Try this
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)       
 {
    // your code to get data 
   // I assumed you are getting data in dataset using some query

   DropDownList ddlUserName = (DropDownList)e.Row.FindControl("ddlUserName");
   ddlUserName .DataSource = dataset.Tables[0].DefaultView;
   ddlUserName .DataValueField = "ValueField"; 
   ddlUserName .DataTextField = "TextField";
   ddlUserName .DataBind();
 }
}


protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {        
           ddlDropDownList.DataSource = dt;
           ddlDropDownList.DataBind();
           ddlDropDownList.DataTextField = "Binds_Field_to_show";
           ddlDropDownList.DataValueField = "Binds_Field_of_Data";
           ddlDropDownList.Items.Insert(0, "--Select--");           
        }

    }


这篇关于如何在asp.net中的行数据绑定事件中绑定gridview中的dropdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆