Prashanth
Prashanth

Reputation: 177

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.

Upvotes: 12

Views: 70760

Answers (5)

Satinder singh
Satinder singh

Reputation: 10198

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
}

Upvotes: 32

Linh Dao
Linh Dao

Reputation: 1663

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

Upvotes: 3

R.Priya
R.Priya

Reputation: 21

protected void gvProject_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            string Active = "";
            if (e.Row.DataItem != null)
            { 
                if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                {
                    Label lblEditActive = (Label)e.Row.FindControl("lblUP_ET_ActiveStatus");
                    if (lblEditActive.Text != string.Empty)
                    {
                        Active = lblEditActive.Text.Trim();
                    }

                    DropDownList ddlActive = (DropDownList)e.Row.FindControl("ddlUP_ET_ActiveStatus");
                    ddlActive.Items.Clear();
                    ddlActive.Items.Add("True");
                    ddlActive.Items.Add("False"); 
                    ddlActive.DataBind(); 
                    ddlActive.Items.FindByText(Active).Selected = true;
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }       

Upvotes: 2

Mohmedsadiq
Mohmedsadiq

Reputation: 133

You have to use RowDataBound event to bind the dropdown control for edited row. Please use below method in RowDataBound event.

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit)
        {
            DropDownList drpcategory1 = (DropDownList)e.Row.FindControl("drpcategory1");
            DataTable dt = con.GetData("Select category_name from category");
            drpcategory1.DataSource = dt;
            drpcategory1.DataTextField = "category_name";
            drpcategory1.DataValueField = "category_name";
            drpcategory1.DataBind();
        }
    }

Hope this will help you.

Upvotes: 0

nunespascal
nunespascal

Reputation: 17724

The event RowEditing occurs just before a row is edited.

You should use the RowDataBound event instead.

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (gv.EditIndex == e.Row.RowIndex && 
       e.Row.RowType==DataControlRowType.DataRow) 
   {       
       DropDownList drpcategory1 = (DropDownList)e.Row.FindControl("drpcategory1"); 
       //bind the control
   }
}

Upvotes: 0

Related Questions