how to bind ASP.Net DropDownList control in EditItemTemplate of GridView on edit(imagebutton)click

后端 未结 5 1934
悲哀的现实
悲哀的现实 2021-01-07 12:21

I have requirement to bind ASP.Net DropDownList control in EditItemTemplate of GridView. I have a edit imagebutton with commandname=\"Edit\"also dropdown needs to be binded

相关标签:
5条回答
  • 2021-01-07 12:37

    Your forgot to bind the dropdown list

    DStatusEdit.DataBind();
    
    0 讨论(0)
  • 2021-01-07 12:47
    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
        }
    
    0 讨论(0)
  • I think this is helpful

    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"));
    }
    
    0 讨论(0)
  • 2021-01-07 12:53

    Code: Try this

    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();
                }
           }
        }
    

    Have already answer similar question Binding dropdownlist inside gridview edititemtemplate

    View Blog Article : How to bind drop-down list inside gridview edit template

    0 讨论(0)
  • 2021-01-07 12:55

    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();
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题