what would be a logic to check if input repeated in gridView?.[https://i.stack.imgur.com/rZ7Yv.png]
Asked
Active
Viewed 103 times
0
-
Handle the `CellValueChanged` event of DataGridView and check for cell content duplicates? – pitersmx Jun 02 '17 at 06:26
-
1Why is this getting voted up? It shows no research effort whatsoever – EpicKip Jun 02 '17 at 06:52
-
1Possible 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 Answers
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 ?
-
-
-
Now, you can call this method in `CellValueChanged` event handler to check the duplicated every time user changes cell input value. – pitersmx Jun 02 '17 at 06:32
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