The
RowDataBound
event should work. You need to check the
RowType
first and then check for the
RowState
. For example:
protected void organizationGridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddlCity = (DropDownList)e.Row.FindControl("CityDropDownListFooter");
}
}
}
Since your
DropDownList
resides within a
FooterTemplate
, then you need check the
RowType
to ensure that you are only manipulating the rows of type
Footer
. Please note that a
GridView
is composed of several row types such as
Header
,
DataRow
,
EmptyDataRow
,
Footer
,
Pager
and
Separator
. The next line in code above is the critical part of the code and that is to determine the
Edit
state.
Accessing the controls from within <edititemtemplate> is a bit tricky, especially if you are not really familiar with how the stuff works within a
GridView
. Equating the
RowState
to
DataControlState.Edit
isn't really accurate and you might get an exception when doing so. The
RowState
property is a
bitwise
combination. Therefore, the
RowState
could indicate that you are in the
Edit
state and the
Alternate
state. Hence, you cannot do a simple equality check when you are in Edit mode. Instead you must do something like this when accessing controls within
EditItemTemplate
.
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
}
We use the bitwise "
&
" operator to determine if the
GridView
is in Edit mode and check the result if its greater than zero. For details about the Bitwise operator see:
Bitwise Operators in C#.[
^]