如何在编辑(图像按钮)单击的GridView的EditItemTemplate中绑定ASP.Net DropDownList控件

时间:2013-09-06 11:27:49

标签: c# asp.net gridview

我需要在GridView的EditItemTemplate中绑定ASP.Net DropDownList控件。 我有一个带有commandname =“Edit”的编辑图像按钮,下拉列表需要从不同的表(目录)绑定,而不是通过绑定网格的表(详细信息)。

if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit && GridView1.EditIndex == e.Row.RowIndex)
 {
    DropDownList DStatusEdit = (DropDownList)e.Row.FindControl("DStatusEdit");
    string query = "select distinct status from directory";
    SqlCommand cmd = new SqlCommand(query);
    DStatusEdit.DataSource = GetData(cmd);
    DStatusEdit.DataTextField = "status";
    DStatusEdit.DataValueField = "status"; DStatusEdit.DataBind();     
    DataStatusEdit.DataBind();
 }

页面正在运行但是当我单击编辑图像按钮时,dropdwn显示但是下拉列表中没有数据,它是空的。 我的gridview在更新面板内。 我怎样才能实现这一目标? 我的案例中的dropdownlist是否应该在rowcommand中绑定,但是如何?

5 个答案:

答案 0 :(得分:3)

代码:试试这个

protected void gv_RowDataBound(object sender, GridViewEditEventArgs e)
    {
     if (e.Row.RowType == DataControlRowType.DataRow)
      {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
              DropDownList ddList= (DropDownList)e.Row.FindControl("DStatusEdit");
              //bind dropdownlist
              DataTable dt = con.GetData("select distinct status from directory");
              ddList.DataSource = dt;
              ddList.DataTextField = "YourCOLName";
              ddList.DataValueField = "YourCOLName";
              ddList.DataBind();

              DataRowView dr = e.Row.DataItem as DataRowView;
              //ddList.SelectedItem.Text = dr["YourCOLName"].ToString();
              ddList.SelectedValue = dr["YourCOLName"].ToString();
            }
       }
    }

已回答类似问题Binding dropdownlist inside gridview edititemtemplate

查看博客文章:How to bind drop-down list inside gridview edit template

答案 1 :(得分:1)

protected void gv_RowDataBound(object sender, GridViewEditEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
      {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
              DropDownList DStatusEdit= (DropDownList)e.Row.FindControl("DStatusEdit");
              DataTable dt = con.GetData("select distinct status from directory");
              DStatusEdit.DataSource = dt;
              DStatusEdit.DataTextField = "status";
              DStatusEdit.DataValueField = "status";
              DStatusEdit.DataBind();
              DataRowView dr = e.Row.DataItem as DataRowView;
              DStatusEdit.SelectedValue = dr["columnname"].ToString();
            }
       }
    }
 }

    protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
    {
      gv.EditIndex = e.NewEditIndex;
      gridviewBind();// your gridview binding function
    }

答案 2 :(得分:0)

您忘了绑定下拉列表

DStatusEdit.DataBind();

答案 3 :(得分:0)

我认为这很有用

private void bind_gridview_with_dropdownlist()
{

    DataSet ds = edtcoursedal.Loaddropdown();      
    foreach (GridViewRow grdRow in GridView1.Rows)
    {
 DropDownList bind_dropdownlist = (DropDownList)(GridView1.FooterRow.Cells[3].FindControl("drplmaincourse"));
            bind_dropdownlist.DataSource = ds;
            bind_dropdownlist.DataTextField = "C_name";
            bind_dropdownlist.DataValueField = "MId";
            bind_dropdownlist.DataBind();
        }

    bind_dropdownlist.Items.Insert(0,new ListItem("Select","0"));
}

你能帮我把那个“select”值绑定到gridview.which意味着如果ddl =“select”我必须得到从datatable到gridview的所有值

答案 4 :(得分:0)

protected void GridView2_RowDataBound(object sender,GridViewRowEventArgs e)     {

    using (SqlConnection con = new SqlConnection(ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT GatePassNo,PurposeOfVisit FROM VisitorList"))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            con.Open();
            DropDownList ddlpurposeofvisit = (DropDownList)e.Row.FindControl("ddlpurposeofvisit");
            ddlpurposeofvisit.DataSource = cmd.ExecuteReader();
            ddlpurposeofvisit.DataTextField = "PurposeOfVisit";
            ddlpurposeofvisit.DataValueField = "GatePassNo";
            ddlpurposeofvisit.DataBind();
            con.Close();
        }
    }

}