Dunador
Dunador

Reputation: 21

GridView (e.Row.RowState & DataControlRowState.Edit) > 0 Not working

What I'm looking to do is, on hitting the Edit button of my gridview, I want to change the propertys of some of the EditItemTemplate controls, based on what has been put in. However the changes haven't been occuring, and after some debugging, I've determined that the code isn't going past the test for whether a row is in edit mode.

So after doing some research, I was told by numerous sites that the way to detect an EditItemTemplate was to use the above code: if((e.Row.RowState & DataControlRowState.Edit) > 0)

The Code in question:

protected void gvSchedule_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string mySelect;
        gvSchedule.EditIndex = e.NewEditIndex;
        mySelect = (some SQL)
        scheduleDataSource.SelectCommand = mySelect;
        scheduleDataSource.DataBind();
    }

protected void gvSchedule_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
               //Altering Code
            }
        }
     }

And the gridView header:

<asp:GridView ID="gvSchedule" runat="server" AlternatingRowStyle-CssClass="GridAlternatingRowStyle" EmptyDataText="There are no item(s) to display." 
            RowStyle-CssClass="GridRo wStyle" HeaderStyle-CssClass="GridHeaderStyle" AllowSorting="True" AutoGenerateColumns="False" 
            DataSourceID="scheduleDataSource" AutoGenerateEditButton="True" OnRowDataBound = "gvSchedule_RowDataBound" OnRowEditing="gvSchedule_RowEditing" >

I'm sure there's something silly I'm overlooking, but I can't seem to find it. I've tried all sorts of different logic statements to attempt to move past that if statement, but it simply won't go. Let me know if you need any other information, I'll put it up. Thanks!

Upvotes: 1

Views: 6340

Answers (5)

nerochco
nerochco

Reputation: 21

value for Alternate and Edit is Alternate|Edit. HasFlag will let you know if your enum value is contained in the current value.

if (e.Row.RowType == DataControlRowType.DataRow)
{
    if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
    {
       //Init Code
    }
    else if (e.Row.RowState.HasFlag(DataControlRowState.Edit))               
   {
      //Alter Code
   }
}

Upvotes: 0

Emad Al-Faiumy
Emad Al-Faiumy

Reputation: 1

if (e.Row.RowType == DataControlRowType.DataRow) {
    if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit) {
        //Altering Code
    }
}

Upvotes: 0

svopex
svopex

Reputation: 11

You can use e.Row.RowState.HasFlag(DataControlRowState.Edit)

instead of (e.Row.RowState & DataControlRowState.Edit) > 0...

It is a better construction...

Upvotes: 1

Nik
Nik

Reputation: 481

You have to check

protected void gvSchedule_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
        if ((e.Row.RowState & DataControlRowState.Edit) != 0)
        {
            //Altering Code            

        }
    }
 }

instead of

protected void gvSchedule_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
           //Altering Code
        }
    }
 }

Upvotes: 0

Jobokai
Jobokai

Reputation: 313

I think the correct syntax for the If statement is:

if ((e.Row.RowState && DataControlRowState.Edit) > 0)
{
  //Altering Code
}

Or if that doesn't work try this:

if((e.Row.RowState > 0) && (DataControlRowState.Edit > 0))
{
  //code here
}

The key being here that you want to use '&&' for AND not just '&'.

Upvotes: 0

Related Questions