12

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.

Tevo D
  • 3,351
  • 21
  • 28
Prashanth
  • 167
  • 2
  • 2
  • 11

5 Answers5

32

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
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Satinder singh
  • 10,100
  • 16
  • 60
  • 102
  • Got the desired thing man.But DataRowView dr = e.Row.DataItem as DataRowView; ddList.SelectedItem.Text = dr["category_name"].ToString();these two lines showed error removing these two lines i executed and got the output dude.Thanks bro – Prashanth Sep 04 '12 at 09:09
  • @Prashanth: good if it help , well can u paste the error msg u getting for those lines. – Satinder singh Sep 04 '12 at 09:32
  • Dude i corrected that error it was just a naming problem but now Bro all is well but now the first element in the dropdownlist is getting rewritten to the label value man when in edit mode (i.e)I have repetition of one element and also losing another.Hope you understand what i mean. – Prashanth Sep 04 '12 at 10:05
  • @Prashanth: Bro check my updated answer have fixed that issue, also Mark upvote if its helps :) – Satinder singh Sep 04 '12 at 11:02
  • 1
    :ya man i also fixed it just now dude.I was about to update your answer man but you updated it dude thanks man. – Prashanth Sep 04 '12 at 12:04
  • Can you post the `Row_Updating()` for the same solution? For Updating, that's where we capture the selected item of the dropdown, don't we? – bonCodigo Jul 15 '14 at 09:26
  • this is exactly what I want – Pramesh Aug 12 '15 at 21:18
3

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();
    }
}
Linh Dao
  • 1,305
  • 1
  • 19
  • 31
2
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;
        }
    }       
Thirumalai murugan
  • 5,698
  • 8
  • 32
  • 54
R.Priya
  • 21
  • 1
0

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
   }
}
nunespascal
  • 17,584
  • 2
  • 43
  • 46
0

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.
Mohmedsadiq
  • 133
  • 1
  • 10