65.9K
CodeProject 正在变化。 阅读更多。
Home

更改 DataGrid 单元格属性

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.69/5 (8投票s)

2006 年 8 月 3 日

2分钟阅读

viewsIcon

84564

downloadIcon

693

更改 DataGrid 单元格的颜色

Sample Image - datagridcellproperties.jpg

我在互联网上搜索过 DataGrid 的示例,以及如何更改一个或多个单元格的属性,例如背景色或字体颜色。很多论坛里的人说这是不可能的,但最终看来却很简单。结合不同的文章和示例,我得到了我想要的解决方案。

引言

本文档描述了如何创建一个 DataGrid,并根据一个或多个条件更改一个或多个单元格的颜色。本文包含非常简单的 ASP.NET 代码。

使用代码

我们想要的是一个包含员工信息的 DataGrid。工资低于 5000 的员工必须以红色显示。我们使用 XML 文件来在 DataGrid 中显示数据。

首先,我们在 aspx 页面上创建一个 DataGrid。

<asp:DataGrid id="dgEmployees" runat="server" AutoGenerateColumns="False" OnItemDataBound="dgBoundItems">
    <AlternatingItemStyle CssClass="datagridAltItem">

    <ItemStyle CssClass="datagridItem">

    <HeaderStyle CssClass="datagridHeader">

</asp:DataGrid>

然后,我们在 PageLoad 中将 XML 文件加载到 DataSet 中。接着,我们在代码后台将列绑定到 DataGrid,以便设置一些属性。

public DataSet ds = new DataSet(); 

private void Page_Load(object sender, System.EventArgs e) 
{
    ///Read the XML-file into the dataset 
    ds.ReadXml(MapPath("employees.xml")); 
    DataView dv1 = new DataView(); 
    dv1.Table = ds.Tables[0]; 
    ///Bind the columns into the datagrid
    BoundColumn datagridcol; 
    datagridcol = new BoundColumn(); 
    datagridcol.HeaderText = "EmpID"; 
    datagridcol.HeaderStyle.Width = Unit.Pixel(100); 
    datagridcol.ItemStyle.Width = Unit.Pixel(100); 
    datagridcol.DataField = "ID"; 
    dgEmployees.Columns.Add(datagridcol); 
    datagridcol = new BoundColumn(); 
    datagridcol.HeaderText = "Name"; 
    datagridcol.HeaderStyle.Width = Unit.Pixel(200); 
    datagridcol.ItemStyle.Width = Unit.Pixel(200); 
    datagridcol.DataField = "Name"; 
    dgEmployees.Columns.Add(datagridcol); 
    datagridcol = new BoundColumn(); 
    datagridcol.HeaderText = "Salary"; 
    datagridcol.HeaderStyle.Width = Unit.Pixel(200); 
    datagridcol.ItemStyle.Width = Unit.Pixel(200); 
    datagridcol.DataField = "Salary"; 
    dgEmployees.Columns.Add(datagridcol); 
    dgEmployees.DataSource = dv1; 
    dgEmployees.DataBind(); 
    dgEmployees.Visible = true; 
}

现在 DataGrid 已经填充了来自 XML 文件的信息。现在我们希望当工资低于 5000 时,工资显示为红色。我们可以在 OnItemDataBound 中实现这一点。当绑定 DataGrid 时,会调用 dgBoundItems 过程。在这里,我们可以检查将要创建的表格的每个单元格。使用GetCellIndex 我们获取表格的列号。我们只需要检查第三列,所以 GetCellIndex 必须等于 2。DataSetIndex 提供行号。行号 0 是标题。我们希望在标题之后开始,所以 DataSetIndex 必须大于 0。最后,我们检查工资是否低于 5000。我们将单元格的前景色更改为红色。

///C#

public void dgBoundItems(object sender, DataGridItemEventArgs e) 
{ 
    foreach(TableCell cell in e.Item.Cells) 
    { 
        ///e.Item.DataSetIndex = row 
        ///e.Item.Cells.GetCellIndex(cell) = column 
        if (e.Item.Cells.GetCellIndex(cell) == 2 && e.Item.DataSetIndex > 0) 
        {
            ///If the salary is less than 5000 the color must be red. 
            if (Convert.ToInt32(cell.Text.Trim()) < 5000) 
            { 
                cell.ForeColor = Color.Red; 
            } 
        } 
    } 
}

结论

这是一种简单的方法来更改单元格中的文本颜色。有了这个示例,就可以更改单个单元格的许多属性。背景颜色、鼠标悬停效果等。

希望我的示例能帮助到很多人。

© . All rights reserved.