【问题标题】:c# windows application GridView Row changing color depending on valuec# windows application GridView Row根据值改变颜色
【发布时间】:2015-01-22 13:08:43
【问题描述】:

我有一个填充的 GridView,我想要检查一个名为“Points”的 int 列中的值,该列现在填充了 Null 值,但有些单元格确实有值,这是由用户给出的。所以它是 null直到它得到一个值。

我希望 GridView Row 在 Points 单元格获取值时更改颜色,因此通过快速查看我可以区分哪些已完成,哪些未完成!

我已经设法为 Name 列制作了一个示例代码,但是当我尝试检查 Points 列时我失败了:S..

代码如下:

   private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    { 
        if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Name")
        {
            if (e.Value != null)
            {
                try{
                string stringValue = (string)e.Value;
                stringValue = stringValue.ToLower();
                if ((stringValue.IndexOf("nikos") > -1))
                {
                    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Pink;
                }
                }catch (InvalidCastException  ex){
            }

            }                        
        }           
    }

如何修改它,以便检查 Points 列中的 null 或 int 值。

谢谢! :D

【问题讨论】:

    标签: c# gridview


    【解决方案1】:
     private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    { 
        if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Points")
        {
            if (e.Value != null)
            {
                 this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Pink;
            }                      
        }           
    }
    

    只需检查 Points 列是否为空,如果不是则设置背景颜色

    【讨论】:

    • 我之前尝试过,感谢您的回答,但如果我这样做,一切都会变成红色,我想 e.Value 作为一个对象只是检查一个单元格是否存在,所以一切都会改变颜色!
    猜你喜欢
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 2015-12-11
    • 1970-01-01
    • 2020-11-13
    • 2020-12-06
    相关资源
    最近更新 更多