关于asp.net:如何动态设置GridView单元格的样式?

How to style a GridView cell dynamically?

我的GridView有3个绑定列:A,B和C。我想以粗体显示这3列的最大值。如何进行比较并将字体设置为粗体(最好在aspx文件中)?谢谢。

1
2
3
4
5
<Columns>                
               


</Columns>

要澄清:A,B和C列中的任何NUMERIC值都可以是最大的,具体取决于行。这是我要设置为粗体的值。

示例:

1
2
3
3   **4**    1
**6**   2    0
**9**   1    2


尝试这种方式:

1
2
3
4
5
6
7
8
9
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int j = 0; j < e.Row.Cells.Count; j++)
        {
            e.Row.Cells[j].Style.Add("BORDER-BOTTOM","#aaccee 1px solid");
            e.Row.Cells[j].Style.Add("BORDER-RIGHT","#aaccee 1px solid");
            e.Row.Cells[j].Style.Add("padding-left","5px");
        }
    }


您需要在代码后面加上这种东西。为此使用RowDataBound

1
2
3
4
5
6
7
8
9
10
11
12
protected void Grid_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int number;
        var highestCells = e.Row.Cells.Cast<TableCell>()
            .Where(tc => int.TryParse(tc.Text, out number))
            .OrderByDescending(c => int.Parse(c.Text));
        foreach(var cell in highestCells)
            cell.Font.Bold = true;
    }
}

您可以在gridview的rowdatabound方法中执行此操作。
理想的方法是从数据库本身获取3列的最大值,然后仅检查rawdatabound中的值。
该网址可帮助您提供一些简短的介绍。与此类似,您可以添加条件并将该列的相应字体样式设置为粗体
http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
在这种情况下,您可以编写此行以粗体显示
e.Row.Cells[2].Font.Bold = true;