【问题标题】:Highlight the null value cells in RadGridView C#突出显示 RadGridView C# 中的空值单元格
【发布时间】:2012-11-13 23:33:00
【问题描述】:
我有一个 radGridView,用户插入行,我想用空值突出显示单元格。例如使单元格的背面颜色为红色。
我尝试了下面的代码,但它没有工作......
在按钮内
for (int j = 0; j < radGridView1.Rows.Count; j++)
{
if (radGridView1.Rows[j].Cells[4].Value.ToString() == "")
radGridView1.Rows[j].Cells[4].Style.ForeColor = Color.Red;
}
那该怎么做呢?或者是否有更好的突出显示方法来通知用户空单元格。
提前致谢
【问题讨论】:
标签:
c#
.net
datagrid
telerik
radgrid
【解决方案1】:
法德,
我会保持干净和重点:
话虽如此,您的代码似乎想要突出显示空值(null 不等于空):
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
if (e.RowIndex != -1)
{
if (e.CellElement.Value != null && e.CellElement.Value.ToString() == "")
{
radGridView1.Rows[e.CellElement.RowIndex].Cells[e.CellElement.ColumnIndex].Style.BackColor = Color.Red;
radGridView1.Rows[e.CellElement.RowIndex].Cells[e.CellElement.ColumnIndex].Style.CustomizeFill = true;
}
}
}
【解决方案2】:
您可以将OnRowDataBound="radGridView1_RowDataBound" 添加到您的网格视图中。每个 gridview 行都会触发此事件。
在后面的代码中可以有以下内容:
public void radGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// do your highlighting here
if (e.Row.Cells[1].Value.ToString() == "")
{
e.Row.ForeColor = Color.Red;
}
}
}
【解决方案3】:
我在ItemDataBound 事件中这样做,像这样:
TableCell myCell = ((GridDataItem)e.Item)["ColumnName"];
...
myCell.ForeColor = Color.ForestGreen;
...