Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Why we write it in grid view row data bound?
if(e.Row.RowType == DataControlRowType.DataRow)
Posted 23-Apr-13 20:51pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Raju,
 
I assume that you need to know about why we are using the below line in row data bound..?
 
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
 
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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:
 
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[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,142
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 24 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100