简体   繁体   中英

ASP.NET help with formatting gridview

im using a gridview to display data taken from a dataset which looks like

NAME | GP   | ORD_GP | EXP   | TOTAL GP | TARGET
a      206     48      -239     15         1600
b      0       27       0        27        1520
TOTAL  206     75      -239     42         3120 

im using TemplateField like so but this can just format values.

    <asp:TemplateField HeaderText="%" ItemStyle-BackColor="Yellow" >
        <ItemTemplate>
            <span class='<%# double.Parse(Eval("PERC_OF_TARGET").ToString()) >= 100 ? "PERC_MoreThan" : "PERC_LessThan"  %>'>
            <%# Eval("PERC_OF_TARGET")%> %
        </span>
    </ItemTemplate>
</asp:TemplateField>

however what I want to do is format the last row (TOTAL) so that the background colour is green.

also is there a way that I can evaluate each item and if it equals to 0 Do not display anything and just leave it blank ?

for making the last row of grid view green:

int r = GridView.Rows.Count;
r--;
GridView.Rows[r].BackColor = System.Drawing.Color.Green;

and to evaluate the value of each cell and make it blank if its 0:

for (int i = 0; i < dt.Rows.Count; i++)
{
    for (int j = 0; j < dataTable.Columns.Count; j++)
    {
        if (Convert.ToInt32(dt.Rows[i][j]) == 0)
        {
            dt.Rows[i][j] = "";
        }
    }
}

hope thats what you needed!

hello dear you can use the grid view prerender event to find the last row and change its background color.

protected void grdData_PreRender(object sender, EventArgs e)
{

    grdData.Rows[grdData.Rows.Count - 1].BackColor = System.Drawing.Color.Red; 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM