Donate

Code In Gridview RowDatabound Not Working If Rowstate Is Alternate|Edit In ASP.NET Web Forms

In a gridview rowdatabound event I have this condition to check if rowstate is in edit mode:
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:
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

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

Post a Comment

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Bootstrap Modal In ASP.NET MVC With CRUD Operations