Asp.net 2.0 FAQ Skills 1

Source: Internet
Author: User
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 ");
}
}
}

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.