Reputation: 4778
I have a DataSet similar to the one below:
ID Name Data1 Data2
1 MM X 1000
2 ST Y 1000
3 EC Z 1000
4 JT T 1000
I display this DataSet
in a DataGridView
by binding the DataSet
. What I would like to do is set the visibility of the Data2
column based on if all the data is similar or not.
So in the above example, I would want to hide the Data2
column because the data represented is common to all elements in the DataSet
. But if any of the elements had a unique entry for that column, I would want the column to be visible.
Is there a way to do this without iterating over the DataSet
? I prefer not to iterate as my DataSet
s are quite large and I want to perform this check for multiple columns.
Upvotes: 2
Views: 1979
Reputation: 46909
var areSame = dt.Rows.Count > 0 &&
dt.AsEnumerable().All(x => dt.rows[0]["Data2"].Equlas(x["Data2"]));
Upvotes: 0
Reputation: 40002
You can use some LINQ to check to see how many distinct values you have in that column:
if(dataTable.AsEnumerable().Select(row => row["Data2"]).Distinct().Count() > 1)
{
// Make column invisible
}
If there is more than 1 distinct value, then you know that not all of the values are equal.
Upvotes: 1