一次只允许数据网格视图中的2个重复值C#?

时间:2017-01-10 01:38:32

标签: c# datagridview

如何将datagridview中的重复项限制为两个?

我查看了这段代码:

// Start check for number of positions being inserted into datagrid 2

        foreach (DataGridViewRow row in optimaldataGridView.Rows)
        {
            if (row.Cells[3].Value.ToString().Contains("PG"))
               MessageBox.Show ("PG has been selected");

        }

但是我需要有类似的东西:

if (row.Cells[3].Value.ToString().Contains("PG")) AND Count > 2 THEN 
    MessageBox.Show ("You have reached your max");

2 个答案:

答案 0 :(得分:1)

谢谢你,这就是Earvin,我能够让它与以下内容合作:

  //Start check for number of positions being insterted into datagrid 2
        int count = 0;
        foreach (DataGridViewRow row in optimaldataGridView.Rows)
        {
            if (row.Cells[3].Value.ToString().Contains("PG"))
            count += 1;
            if (count > 2)
                {

                MessageBox.Show("You have exceeded your max");

            }
        }

答案 1 :(得分:0)

创建变量计数,并在数据网格上有PG时添加1

 foreach (DataGridViewRow row in optimaldataGridView.Rows)
    {
        if (row.Cells[3].Value.ToString().Contains("PG"))
           MessageBox.Show ("PG has been selected");
           count+=1;
    }

如果count> = 2 {

msgbox("您已达到最大值");

}