0

what would be a logic to check if input repeated in gridView?.[https://i.stack.imgur.com/rZ7Yv.png]

Usman
  • 83
  • 1
  • 3
  • Handle the `CellValueChanged` event of DataGridView and check for cell content duplicates? – pitersmx Jun 02 '17 at 06:26
  • 1
    Why is this getting voted up? It shows no research effort whatsoever – EpicKip Jun 02 '17 at 06:52
  • 1
    Possible duplicate of [C# Find duplicate Values in Datagridview](https://stackoverflow.com/questions/10279723/c-sharp-find-duplicate-values-in-datagridview) – EpicKip Jun 02 '17 at 06:53

2 Answers2

2
 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())
        }

will this help you ?

Mahesh
  • 8,694
  • 2
  • 32
  • 53
Vicky S
  • 762
  • 4
  • 16
0

You also can do it like this in one query:

        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();
Timon Post
  • 2,779
  • 1
  • 17
  • 32