How to check for combined RowState 'Altering | Edit' in RowDataBound?

Viewed 2273

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):

enter image description here

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?

2 Answers

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
}
Related