Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding dropdownlist inside gridview edititemtemplate

I'm not able to bind my dropdownlist present in edititem template . I am getting null reference when i try to access it.

My design:

<asp:TemplateField HeaderText ="Category">
    <ItemTemplate >
    <asp:Label ID="drpcategory" Text ='<%#Bind("category") %>' runat ="server" />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:DropDownList ID="drpcategory1"  AppendDataBoundItems="True" runat="server" >
        </asp:DropDownList>
    </EditItemTemplate>
</asp:TemplateField> 

My code behind:

protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
    gv_table1.EditIndex = e.NewEditIndex;
    DropDownList drpcategory1 = ((DropDownList)gv_table1.Rows[e.NewEditIndex].Cells[8].FindControl("drpcategory1"));
    //BindDropDown(drpcategory1);
    dt = con.GetData("Select category_name from category");

    String str = gv_table1.Rows[e.NewEditIndex].FindControl("drpcategory1").GetType().ToString();
    //((DropDownList)gv_table1.Rows[e.NewEditIndex].Cells[8].FindControl("drpcategory1")).DataSource = dt;
    drpcategory1.DataSource = dt;
    drpcategory1.DataTextField = "category_name";
    drpcategory1.DataValueField = "category_name";
    drpcategory1.DataBind();

    this.setgrid();
}

I've tried looking on the net and tried many things in vain. I am new to asp. Thanks in advance. I would like the dropdown to be bound only when user enters edit mode.

like image 612
Prashanth Avatar asked Sep 03 '12 11:09

Prashanth


2 Answers

I do it like this. In which, Name and Id are two fields of Company object:

HTML Code:

<asp:TemplateField HeaderText="Công ty">
    <EditItemTemplate>
        <asp:DropDownList ID="ddlCompanyEdit" DataSource="<%# PopulateddlCompanyEdit() %>" DataValueField="Id" DataTextField="Name" runat="server"></asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lbCompany" runat="server" Text='<%#Bind("Company") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

C# code behind:

protected IEnumerable<Company> PopulateddlCompanyEdit()
{
    using (var bkDb = new BrickKilnDb())
    {
        return bkDb.Companies.ToList();
    }
}
like image 89
Linh Dao Avatar answered Sep 28 '22 16:09

Linh Dao


Code Behind: Tested Code and also set dropdown-list selected value on edit mode

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            DropDownList ddList= (DropDownList)e.Row.FindControl("drpcategory1");
            //bind dropdown-list
            DataTable dt = con.GetData("Select category_name from category");
            ddList.DataSource = dt;
            ddList.DataTextField = "category_name";
            ddList.DataValueField = "category_name";
            ddList.DataBind();
            
            DataRowView dr = e.Row.DataItem as DataRowView;
            //ddList.SelectedItem.Text = dr["category_name"].ToString();
            ddList.SelectedValue = dr["category_name"].ToString();
        }
    }
}
    
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
    gv.EditIndex = e.NewEditIndex;
    gridviewBind();// your gridview binding function
}
like image 32
Satinder singh Avatar answered Sep 28 '22 16:09

Satinder singh