Data conversion during display of the gridview template Column

Source: Internet
Author: User
When displaying data, the gridview control usually displays some numbered data in the database as the corresponding name. If the numbers and names correspond to data managed through tables in the database, when you bind data to the gridview, You can include the name field in the data source. If the number and name are not managed by the data table, they are fixed. For example, 1 indicates the freshman class, and 2 indicates the second shift, in this case, the rowdatabind event of the gridview control needs to be processed accordingly.
If the data is displayed in the Bind Column of the girdview control, the conversion is simple. See the following code:

Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
// Convert the class ID to the class name
If (E. Row. cells [5]. Text = "1") // 5 is the sequence number of the column to be converted, starting from 0.
{
E. Row. cells [5]. Text = "Freshman Class ";
}
Else if (E. Row. cells [5]. Text = "2 ")
{
E. Row. cells [5]. Text = "secondary shift ";
}
}
}

However, if the data is displayed in the template column of the gridview control, the preceding method cannot be used. You can refer to the following code: Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
// Convert the class ID to the class name
Label LBL = (Label) E. Row. findcontrol ("label7"); // use the template Column
If (LBL! = NULL)
{
If (LBL. Text. Trim () = "1 ")
{
LBL. Text = "Freshman Class ";
}
Else if (LBL. Text. Trim () = "2 ")
{
LBL. Text = "secondary shift ";
}
}
}

"Label7" is the ID of the label control used to display data in the template column.

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.