Monday, March 9, 2009

DropDownList inside Gridview

Hi All,

It is very common to have child controls inside the Gridview.Among them Drop down List is very common.Drop down list inside the gridview and handling their selected index changed event becomes inevitable.

We know that Drop Down list does not Support Command Name property so we cant handle the event in the Row Command event.

Here is the solution of it...which is used very very commonly...

protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e) {

// get reference to the row

GridViewRow gvrow = (GridViewRow)(((Control)sender).NamingContainer);

// Get the reference of this DropDownlist

DropDownList ddl1= (DropDownList) gvrow.FindControl("dropdownlist1");

// Get the reference of other DropDownlist in the same row.

DropDownList ddl2= (DropDownList) gvrow.FindControl("dropdownlist2");

}


Thanks,

Nitin Sharma