Reputation: 1107
As the topic says: Is there a way to return a list of distinct values from a certain Column of a DataGridView?
Upvotes: 4
Views: 10994
Reputation: 54433
This should do what you asked for:
var vv = dataGridView1.Rows.Cast<DataGridViewRow>()
.Select(x => x.Cells[yourColumn].Value.ToString())
.Distinct()
.ToList();
Note that the simple version above assumes that there are only valid values. If you also may have new rows or empty cells you may want to expanded it like this:
var vv = dataGridView1.Rows.Cast<DataGridViewRow>()
.Where(x => !x.IsNewRow) // either..
.Where(x => x.Cells[column].Value != null) //..or or both
.Select(x => x.Cells[column].Value.ToString())
.Distinct()
.ToList();
Upvotes: 13