Saturday, December 25, 2010

Code in gridview rowdatabound not working if rowstate is (Alternate|Edit)

In a gridview rowdatabound event I have this condition to check if rowstate is in edit mode:
Code:
if (e.Row.RowState == DataControlRowState.Edit) //row is in edit mode  
  {  
      TextBox actdaterec = (TextBox)e.Row.Cells[1].FindControl("txtDateRec");  
      actdaterec.Text = string.Empty;  
      TextBox cashRec = (TextBox)e.Row.Cells[2].FindControl("txtCashReceived");  
      cashRec.Text = string.Empty;  
  }  
The code above usually works if the rowstate is in edit mode, however on the succeeding rows the rowstate becomes Alternate|Edit. The code above does not work. So the solution would involve C# bitwise operator (&).
See the revised code below:
Code:
if ((e.Row.RowState & DataControlRowState.Edit) > 0) //row is in edit mode  
 {  
    TextBox actdaterec = (TextBox)e.Row.Cells[1].FindControl("txtDateRec");  
    actdaterec.Text = string.Empty;  
    TextBox cashRec = (TextBox)e.Row.Cells[2].FindControl("txtCashReceived");  
    cashRec.Text = string.Empty;  
 }  
The code will execute even if rowstate is Alternate|Edit. It will ignore the alternate value.
Reference: ASP.NET Forum

3 comments:

  1. I have done the same thing but the problem is in edit mode its not even going in rowdatabound function

    ReplyDelete
    Replies
    1. Hi Danial,

      This solution should work given the succeeding rows are in Alternate|Edit mode. Try debugging the rowdatabound event to check the rowstate of the succeeding rows.

      Delete