Click here to Skip to main content
15,921,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,



I have to bind dropdownlist from a table in edit item template of gridview at code behind .I don't want to use SqlDataSource .

I used something like below

protected void GRVAccess_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (GRVAccess.EditIndex == e.Row.RowIndex)
{
try
{
DataTable dt1;

dt1 = desg.BindDesignation();
if (dt1.Rows.Count > 0)
{
DropDownList ddldesg = (DropDownList)e.Row.Cells[0].FindControl("ddlDesg");
ddldesg.DataSource = dt1;
ddldesg.DataTextField = "Designation";
ddldesg.DataValueField = "DesgId";
ddldesg.DataBind();
//ddldesg.Items.Insert(0, "Select");
}
}
catch (Exception ex)
{

lblErrorMessage.Visible = true;
lblErrorMessage.Text = ex.Message;
}
}
}

But it's showing Object reference not set to an instance of an object at ddldesg.DataSource = dt1;
Please help me.

Thanking You
mohd Wasif
Posted
Updated 20-May-11 2:29am
v2

 
Share this answer
 
v2
Use following code to achieve this:

<br />
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)<br />
        {<br />
            Control ctrl = e.Row.FindControl("ddlTest");<br />
            if (ctrl != null)<br />
            {<br />
                DropDownList ddl = (DropDownList)ctrl;<br />
                List<Users> ls = GetDropDownData();<br />
                ddl.DataSource = ls;<br />
                ddl.DataTextField = "Name";<br />
                ddl.DataValueField = "Id";<br />
                ddl.DataBind();<br />
            }<br />
        }<br />
 
Share this answer
 
XML
<        EditItemTemplate>
                    <asp:DropDownList ID="ddlGender" runat="server" >
                    <asp:ListItem Text="male" Value="male" />
                     <asp:ListItem Text="female" Value="female" />
                     </asp:DropDownList>
                     </EditItemTemplate>




then use
<code>
DropDownList DdlGender = (DropDownList)row.FindControl("ddlGender");

string Gender = DdlGender.SelectedValue.ToString();

now you can use gender
 
Share this answer
 
v2
Comments
Mohd Wasif 20-May-11 8:33am    
I have bind dropdown from database .I mentioned Clearly

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900