During the data display process, we often modify the display format based on the displayed content to achieve the functions of prompting, emphasizing, or others. In the gridview, you can use the corresponding CSS to determine the data to achieve this effect. This method can be used in a series of template controls, such as detailsview, formview, and repeator.
Let's take a look:
In this gridview, the recommended movies on the home page are highlighted based on the value of the homepage recommendation. The method is as follows: first, bind the data in objectdatasource to the gridview normally, and preview it to see if it can display data normally. If the display is normal, proceed to the next step.
After each row is bound, the rowdatabound event is triggered. At this time, the bound data can be obtained through the gridviewroweventargs type parameter E. After obtaining the parameter, determine whether to apply CSS to it based on whether the value is true. Code As follows: Protected Void Gridview1_rowdatabound ( Object Sender, gridviewroweventargs E)
{
If (E. Row. rowtype = Datacontrolrowtype. datarow)
{
Qmxmovie. movierow current = (Qmxmovie. movierow) (datarowview) E. Row. dataitem). row;
If (Current. Index) // Homepage recommendations
{
E. Row. cssclass= "Datarowemp";
}
}
}
Attach the corresponding CSS code:. Datarowemp
{}{
Background-color:# Bce3ff;
}
Haha, a few lines of code, a color scheme, helps you show your thoughts and achieve a perfect user experience (how is it like advertising? ^_^ ). Of course, you only need to enrich the CSS to achieve more results. For example, to display in bold, you can modify the CSS as follows: . datarowemp
{} {
background-color : #bce3ff ;
font-weight : bold ;
}