A common application scenario is that in the gridview, when the inventory is smaller than a certain number, the background color changes first.
The sum of the statistics for a column is displayed in the footer.
First, when the stock is smaller than a certain value, the background color of the row changes, such
Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
// Confirm the value of the inventory field.
//
// We use a databinder. eval () call from
// Obtain the value of the inventory field in the data and pass it to the first parameter of databinder. eval ()
// The number is the data (E. Row. dataitem) that will be bound to the gridview data column ),
// The second parameter passed to databinder. eval () is the field name.
Decimal stock =
Convert. todecimal (databinder. eval (E. Row. dataitem, "inventory "));
If (stock <= 0)
{
// If the inventory is smaller than or equal to 0, set the background color of the data column to red.
E. Row. backcolor = color. Red;
}
Decimal totalmoney =
Convert. todecimal (databinder. eval (E. Row. dataitem, "order amount "));
If (totalmoney> 0)
{
// If the order amount is greater than 0, set the background color of the data column to yellow.
E. Row. backcolor = color. Yellow;
}
// Accumulate the order amount and set it to the variable ordertotal.
Ordertotal + = totalmoney;
}
}
Add the footer template to the page.
<Asp: templatefield headertext = "order amount" sortexpression = "order amount">
<Edititemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # eval ("order amount", "{0: c }") %> '> </ASP: Label>
</Edititemtemplate>
<Footertemplate>
<Asp: Label id = "ordertotallabel" runat = "server" font-underline = "true" forecolor = "red"> </ASP: Label>
</Footertemplate>
<Itemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # BIND ("order amount", "{0: c }") %> '> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>
// Create a variable to store the order amount and sum.
Private decimal ordertotal = 0.0 m;
Protected void gridview1_rowcreated (Object sender, gridviewroweventargs E)
{
// Extract the current data column.
Gridviewrow ROW = E. row;
// If the data column being created is the end of a page, the updated data row is added to the total number.
If (row. rowtype = datacontrolrowtype. footer)
{
// Obtain the tag control ordertotaltotal at the end of the page.
Label Total = (Label) (E. Row. findcontrol ("ordertotallabel "));
// The order amount and total amount are displayed in currency format.
If (total! = NULL)
{
Total. Text = ordertotal. tostring ("C ");
}
}
}