asp.net how to get a column value from a row in the GridView

Source: Internet
Author: User

The best way to change its style based on the value of a column is to think of ways to datarowbound the GridView event. The Datarowbound event is executed immediately after the rows in the GridView are bound to the data. The Datarowbound event uses the GridViewRowEventArgs class as the event variable. You can use the GridViewRowEventArgs property to manipulate rows that have been bound to data through event variables.

Copy Code code 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 RowType property of the GridView can get the row type of the current row. RowType is a set of Datacontrolrow enumerations.
Look at the following code example to detect whether the row listed by the GridView is a standard type of row.

Copy Code code as follows:


protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)


{


if (E.row.rowtype = = Datacontrolrowtype.datarow)


{


//do something!


}


}


You can use the Cells property of the row to get its cells, which returns a Tablecellcollection object. The specific cells is then obtained through the tablecellcollection index. The tablecellcollection index returns a Tabelcell object that corresponds to a cell in the row:

Copy Code code as follows:


protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
String value = E.row.cells[0]. Text;
}
}

Related Article

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.