【问题标题】: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】:

    法德,

    我会保持干净和重点:

    • 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;
            }
    
        }
    }
    

    【讨论】:

      【解决方案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;
        ...
        

        【讨论】:

          猜你喜欢
          • 2021-02-26
          • 1970-01-01
          • 2019-07-02
          • 2015-09-13
          • 2016-08-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多