【问题标题】:Check if value is repeated in gridview [closed]检查gridview中的值是否重复[关闭]
【发布时间】:2017-06-02 14:53:21
【问题描述】:

检查输入是否在 gridView 中重复的逻辑是什么?[https://i.stack.imgur.com/rZ7Yv.png]

【问题讨论】:

标签: c# .net


【解决方案1】:
 private bool DuplicateExist()
        {
            var existingValues = dataGridView1.Rows
                                  .OfType<DataGridViewRow>()
                                  .Where(x => x.Cells["Bar Code"].Value != null)
                                  .Select(x => x.Cells["Bar Code"].Value.ToString())
            return (existingValues.Count != existingValues.Distinct().Count())
        }

这对你有帮助吗?

【讨论】:

  • 你不需要ToList()
  • 是的,谢谢@pitersmx
  • 现在,您可以在CellValueChanged 事件处理程序中调用此方法,以在每次用户更改单元格输入值时检查重复项。
【解决方案2】:

您也可以在一个查询中这样做:

        dataGridView1.Rows.Add("254");
        dataGridView1.Rows.Add("124");
        dataGridView1.Rows.Add("543");
        dataGridView1.Rows.Add("234");
        dataGridView1.Rows.Add("254");

        bool anyDuplicated = dataGridView1.Rows
            .OfType<DataGridViewRow>()
            .Where(x => x.Cells["Column1"].Value != null)
            .Select(x => x.Cells["Column1"].Value.ToString())
            .GroupBy(x => x)
            .Where(g => g.Count() > 1)
            .Select(g => g.Key)
            .Any();

【讨论】:

    猜你喜欢
    • 2017-08-29
    • 2014-09-27
    • 1970-01-01
    • 2013-05-11
    • 2014-11-29
    • 1970-01-01
    • 2016-02-14
    • 2017-05-29
    • 1970-01-01
    相关资源
    最近更新 更多