Najjaf Shuja

Najjaf Shuja

  • NA
  • 3
  • 5.7k

Can't access gridview row in dropdown selected index changed

Jun 4 2014 7:40 AM
Hi, I am developing a web application using asp.net, c#.
I have simple gridview control having dropdown in it. in gridview rowdatabound event, i call dropdown selected index changed event. there i get gridview row but exception comes.
My code is below.
 
protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
  table1 obj = e.Row.DataItem as table1;
DropDownList ddldepartment = e.Row.FindControl("ddldepartment ") as DropDownList;

/// Bind Dropdownlists
#region DDL Department

ddldepartment.DataSource = new x_SystmController().GetAllDepartments();
ddldepartment.DataTextField = "Title";
ddldepartment.DataValueField = "PK_id";
ddldepartment.DataBind();
ddldepartment.Items.Insert(0, new ListItem("--- Select ---", "0"));

#endregion

if (obj.fk_department_id > 0)
{
ddldepartment.SelectedValue = obj.fk_department_id.ToString();
ddlSystem_SelectedIndexChanged(this, new EventArgs());             // It will call the index changed event of ddl written below
}
 
 
protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);        // Here i get null value
Label lbName = gvr.FindControl("lbName") as Label;                                             // Here exception comes object reference is not set to the instance
 
Please help....
Thanks in advance..!!!!

Answers (2)