On my page, I set up a GridView with a few columns. I coded an update, delete and insert method. While my GridView binds its data, the following method is called:
protected void GridViewAutomat_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.RowState != DataControlRowState.Edit)
{
//some code
}
}
}
The Problem with my second if statement is that when my GridView is entering the Edit Mode (when I want to update a record in my GridView) it doesn't catch the RowState
Alternate | Edit which looks like this(this is how the RowState is after I call my Update Method):
When I try to combine the two RowStates separately it wont work either:
if(e.Row.RowState != DataControlRowState.Edit &&
e.Row.RowState != DataControlRowState.Alternate)
The Code in the if-statement should be executed when the row is not in edit (Alternate | Edit) mode, that's why I have !=
as an operator
Does anyone know how I can catch the combined mode Alternate | Edit and the Edit Mode together?
You can check it in:
Edit mode:
e.Row.RowState != DataControlRowState.Edit
Alter | Edit mode:
e.Row.RowState != (DataControlRowState.Edit | DataControlRowState.Alternate)
In workaround:
if (e.Row.RowType == DataControlRowType.DataRow &&
e.Row.RowState != DataControlRowState.Edit &&
e.Row.RowState != (DataControlRowState.Edit | DataControlRowState.Alternate))
{
//... Here is logic
}
Or:
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState != DataControlRowState.Edit &&
e.Row.RowState != (DataControlRowState.Edit | DataControlRowState.Alternate))
{
//... here is logic
}
}
The RowState
value is a DataControlRowState
enum, which has the Flags
attribute set. This means we can call Enum.HasFlag
, like so:
if (e.Row.RowType == DataControlRowType.DataRow &&
e.Row.RowState.HasFlag(DataControlRowState.Edit))
{
//... Here is logic
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With