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.