C#dataGridView控件如何在当前环境中使用Contains包含来判断特定字符?

指定列名是"员工状态",当表中该列值对应存在 "离职"字符时,设置行底色
想问一下,如何在当前环境中使用Contains包含来判断特定字符?
求解答,谢谢!


foreach (DataGridViewRow r in dataGridView1.Rows)
            {
               if ((string)(r.Cells["员工状态"].Value) == "5_离职")
                    {
                        r.DefaultCellStyle.ForeColor = Color.Red;
                    }
                }

                //以下命令无效
                //if (r.Cells["员工状态"].ToString().Contains("离职"))
                //{
                //    r.DefaultCellStyle.ForeColor = Color.Red;
                //}
            }
if (r.Cells["员工状态"].Value.ToString().Contains("离职"))试看

你调试过 r.Cells["员工状态"].ToString() 的值吗?

有尝试过,因初学对多窗体切换无法有效跟踪调试,放弃了。
当前也只是尝试,结果不成功,想找能成功的表述语句。

先判断是否为null,不为null再判断是否包含

已确认过该列所有行均有值,非空值。
#1处上半部分用等于办法是成功的。
现只是想要找包含的办法,因为用处更大。

当前就是不知道怎么回事啊。
先确认一下:
 if (r.Cells["员工状态"].Value.ToString().Contains("离职"))
这个表达式是否正确?

原始语句
if ((string)(r.Cells["员工状态"].Value) == "5_离职")
遇到Null值时会直接报错的。当前运行时没有出错。

我们通常在bind时让微软自己处理,而不是自己循环

你可以在RowPostPaint,CellFormart这类事件里处理

比如 CellFormatting
 private void dgvResult_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    //在这里直接判定是不是想要列,值是什么,如果列和值都符合条件,修改 e.CellSytle
}换个方式试看能否取到有效值
.Rows[1].Cells[1].Value

换个方式试看能否取到有效值:.Rows[1].Cells[列++].Value

随便在项目里摘一段代码,当然这个代码只是修改了当前Cell,如果要修改整行可以自己修改

     private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                if (e.Value == null)
                    return;
                if (e.ColumnIndex == 5 || e.ColumnIndex == 6 || e.ColumnIndex == 7 || e.ColumnIndex == 8)
                {
                    bool tes = (bool)e.Value;
                    e.Value = tes ? "开" : "关";
                    e.CellStyle.BackColor = tes ? Color.LawnGreen : Color.Yellow;
                }

                if (e.ColumnIndex == 1)
                {
                    int temp = (int)e.Value;
                    if (temp > 0)
                    {
                        Enum.TryParse<CommonDefine.dayofweekCn>(temp.ToString(), out var dayOfWeek);
                        e.Value = dayOfWeek;
                    }
                }
            }
        }

有报错:
“System.Windows.Forms.DataGridViewRow”不包含“Rows”的定义,并且找不到可接受类型为 “System.Windows.Forms.DataGridViewRow”的第一个参数的扩展方法“Rows”(是否缺少 using 指令或程序 集引用?)

基本查错都不会么,报错时把鼠标一个一个移到变量上面,看哪个值为null了

要取值在第几列就写第几列的序号r.Cells[?]

如下能取到单元格的值不?只要能取到单元格的值就能用包含判断。
dataGridView1.Rows[1].Cells[列++].Value
超纲?完全不会的,从net1.1微软第一次把net折腾出来的时候,第一本红宝书,第一次msdn讲dgv的时候就是这样。
也就是第一本微软的教程就这么写的,你说他超纲?现在你的写法,其实是XX园时代留下的印记,因为当年很多人是asp,vb过来的,他把asp,vb年代做法搬过来了(有时候属于迫不得已,资料少,又要急着完成,在烂的实现也是实现)

上午要开会,乘着等开会的时间,3分钟写个(这次选择RowPrePaint事件,实际上很多事件都可以完成这件事)

  private void Form2_Load(object sender, EventArgs e)
        {
            List<MyClass> lst = new List<MyClass>();
            lst.Add(new MyClass());
            lst.Add(new MyClass()
            {
                age = 18,name = "木子李"
            });
            lst.Add(new MyClass()
            {
                age = 18,name = "赵日天"
            });
           

            dataGridView1.RowPrePaint += DataGridView1_RowPrePaint;
            dataGridView1.DataSource = lst;
        }

        private void DataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                MyClass data = dataGridView1.Rows[e.RowIndex].DataBoundItem as MyClass;
                if ((data.name??"").IndexOf("李")>-1)
                {
                    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
                }
            }
        }

如果你不想通过注册事件来完成,我认为你需要好好检查一下DataGridViewRow 和DataGridViewCell的值,这俩肯定有一个是null值,建议你把表格的界面发上来看一下,我觉得是你r.Cell[""]的问题

if(r.Cells[7].Value.ToString().Contains("离职"))
这个用法试过,怕自己数错,也用过8,都是一样的报错。

我之前做過類似的判斷, 我不知道你的儲存格格式下拉選單(combobox)還是文字(text)
我判斷的是數字大小,用迴圈判斷每一個row的Cells["totalprice"]
如果都是"5_離職"的話,看是否完全符合就直接 == 判斷就好, 如果是不是特殊字元的話可以加@"5_離職"

***你可以確認一下,Cells["XXXX"] 裡面打的是不是ColumnName ; 而不是text的內容

for (int i = 0; i < dataGridView1.Rows.Count; i++) 
                {
                    if (int.Parse(dataGridView1.Rows[i].Cells["totalprice"].Value.ToString()) < int.Parse(mintotal.Text)) 
                    {
                        dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Coral;
                    }
                    else if (int.Parse(dataGridView1.Rows[i].Cells["totalprice"].Value.ToString()) > int.Parse(maxtotal.Text))
                    {
                        dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.DeepSkyBlue;
                    }
                    else 
                    {
                        dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
                    }
                }

可以套用,谢谢!!!
这样一来,我又有两个问题想问一下:
1、下列中的 if (!(r.Cells["员工状态"].Value is System.DBNull))
www.9iBee.com不应该是完成了空值的判断了吗?
之前在别的查询表里,那个列存在很多空值,用#1的法子是可行的。一换成Contains就出现问题。
在这里已反复确认不存在空白值,怎么就用不了了?

//满足特定条件的行记录字体、颜色修改
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                if (!(r.Cells["员工状态"].Value is System.DBNull))//当字段值不为空时,有效。2020.04.02
                {
                    if ((string)(r.Cells["员工状态"].Value) == "5_离职")
                    {

 

2、我的每个查询后面都搭配上了dateGridView条件


            //dataGridView1设置
            dataGridView1.AllowUserToAddRows = false;//禁止最后一行空白
            dataGridView1.AllowUserToDeleteRows = false;//禁用delete键的删除功能
            dataGridView1.ReadOnly = true;//禁用单元格编辑功能
这个禁止最后一行空白,也不能解决null的问题吗?

这样一来,我又有两个问题想问一下:
1、下列中的 if (!(r.Cells["员工状态"].Value is System.DBNull))不应该是完成了空值的判断了吗?
之前在别的查询表里,那个列存在很多空值,用#1的法子是可行的。一换成Contains就出现问题。
在这里已反复确认不存在空白值,怎么就用不了了?

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值