Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why we write it in grid view row data bound?
if(e.Row.RowType == DataControlRowType.DataRow)
Posted

Hi Raju,

I assume that you need to know about why we are using the below line in row data bound..?

C#
if(e.Row.RowType == DataControlRowType.DataRow)


If so, then follow it here.

We have different kinds of rows like Header row, Footer row, Data row, Empty Data Row, Pager and Seperator. These are the kinds of rows that we can generate in Gridview and use them as per our usage. But most of the time, we only work with the data row in order to show the data, manipulate the data, add CSS to that data row, customize the data in a row, but not for header rows and other above specified rows.

This row databound event will be called when ever we call

C#
GridView1.DataBind();


So, for every kind of row that you have in your gridview (that you'll specify in the designer part like ShowFooter="true", ShowHeader="true"), this event is fired. We'll have all the logic in this event and we want that to be called only for DataRow and not for all other kinds of row which will obvisouly dont go inside the if condition that you specified above.

If we dont use that condition, it'll try to run the code for other rows also, and cause errors.

Hope you got my point.! If its not what ou wanted, let me know.

Thank you,
Vamsi
 
Share this answer
 
It's to verify that row (row in a gridview or detailsview), is of type dataRow.

Gridview has different types of rows.
1) Header
2) DataRow
3)Footer
4) Pager
5) Separator.
6) EmptyDataRow

Based on what is supposed to be accessed, RowType is mentioned in RowDataBoundEvent

If you want to access footer and display calculated Total, you can write like:
C#
private Decimal orderTotal = 0.0M;
if (row.RowType == DataControlRowType.Footer)
    {
      // Get the Label control in the footer row.
      Label total = (Label)e.Row.FindControl("lblTotal");
      // Display the grand total of the order formatted as currency.
      if (total != null)
      {
        total.Text = orderTotal.ToString("c");
      }
    }


Similarly if you want to access data inside the gridview, you can write like:
C#
if (row.RowType == DataControlRowType.DataRow)
{
   //Your code changes/manipulations here.
}


Please go through following link for more information:
system.web.ui.webcontrols.gridviewrow.rowtype.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900