Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 899
Alessandro Minneci Avatar asked Sep 15 '17 08:09

Alessandro Minneci


2 Answers

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
    }
}
like image 84
youpilat13 Avatar answered Nov 10 '22 19:11

youpilat13


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
}
like image 39
Ian Avatar answered Nov 10 '22 18:11

Ian