繁体   English   中英

突出显示RadGridView C#中的空值单元格

[英]Highlight the null value cells in RadGridView C#

我有一个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;  
}

那怎么办呢? 或者如果有更好的突出显示方法通知用户空单元格。

提前致谢

Fadddd,

我会保持清洁和重点:

  • 03Usr的答案是谈论一排,你想要个别细胞

  • Cyber​​Dude的答案没有考虑到Telerik的DGV修改外观和功能的许多事件

话虽如此,您的代码似乎想要突出显示空值(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;
        }

    }
}

在此输入图像描述

我在ItemDataBound事件中执行此操作,如下所示:

TableCell myCell = ((GridDataItem)e.Item)["ColumnName"];
...
myCell.ForeColor = Color.ForestGreen;
...

您可以将OnRowDataBound="radGridView1_RowDataBound"添加到gridview。 每个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;
        }

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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