How does asp.net obtain the value of a column in a row in the GRIDVIEW?

Source: Internet
Author: User

The best way to change the style of a column based on its value is to find a solution in the DataRowBound event of the GridView. After the row in the GridView is bound to data, the DataRowBound event is executed immediately. The DataRowBound event uses the GridViewRowEventargs class as the event variable. With event variables, you can use the GridViewRowEventArgs attribute to operate rows that have been bound to Data. Copy codeThe Code is as follows: protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
GridViewRow row = e. Row;
}

Row returns a GridViewRow object in the TableRow class.
The bound Row has several different types. For example, DataRow, EmptyDataRow, Footer, Header, Pager, and Separator. The row type of the current row can be obtained through the RowType attribute of the GridView. RowType is a set of DataControlRow enumeration.
Check the following code example to see if the row listed in the GridView is a standard row.Copy codeThe Code is as follows: protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
If (e. Row. RowType = DataControlRowType. DataRow)
{
// Do something!
}
}

You can use the Cells attribute of Row to get its Cells, which will return a TableCellCollection object. Then the TableCellCollection index is used to obtain the specific Cells. TableCellcollection indexes return a TabelCell object, corresponding to a Cell in the Row:Copy codeThe Code is as follows: protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
If (e. Row. RowType = DataControlRowType. DataRow)
{
String value = e. Row. Cells [0]. Text;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.