如何以编程方式格式化RadGrid单元格

5

我需要根据单元格的值来格式化(radgrid的背景颜色,前景颜色,字体样式)。

例如,如果值为负数,则将该单元格的前景色设置为红色。

请问有谁能告诉我如何实现这个功能?

3个回答

8
 protected void grdName_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            if (Convert.ToInt32(((DataRowView)item.DataItem)["Column"]) < value)
            {
                TableCell cell = item["Column"];
                cell.BackColor = Color.PeachPuff;
            }
        }
    }

不知道为什么有人给我点了踩,我的回答与其他人的不同,所以我觉得应该补充一下。 - live-love
也许他们不喜欢 PeachPuff。 - JohnnyBizzle

4
在您的aspx页面中,将onItemDataBound="Data_OnitemDataBound"添加到radGrid声明中。
然后在代码后台添加以下内容。Cells[]中的数字是您想要修改或验证的列的索引。
protected void Data_OnItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        if (Convert.ToDecimal(item.Cells[3].Text) < 0)
        {
            item.Cells[3].ForeColor = System.Drawing.Color.Red;
        }
    }
}

2
以下代码可用于RadGrid中的所有单元格。
  protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        foreach (GridDataItem dataItem in RadGridProduct.MasterTableView.Items)
        {
            int cellCount = dataItem.Cells.Count;

            foreach (GridTableCell item in dataItem.Cells)
            {
                if (item.Text == null ||Convert.ToInt32(item.Text) < 0 )
                    item.BackColor = System.Drawing.Color.Brown;
            }

        }

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接