tags:

views:

639

answers:

3

I have a datatable whose columns are lets say col1,col2,col3 Now I want to validate each column value whether its 0 or not

I do not want to use if else clause that will be writing to much stuff.

Is there any possibility to do the same task using HashTable or something else ?

A: 

Loop over the Columns and store the columns which have value 0 in a List<DataColumn>.

Gerrie Schenck
A: 

You could create an event handler for the OnColumnChanged Event and check that the value is not zero.

REA_ANDREW
A: 

If you want to know if any column is 0, then:

bool haveZero = (col1 == 0) || (col2 == 0) || (col3 == 0)

if you need to know if all are zero, then replace ors with ands.

If you are trying to work out if any value in a column is zero, then you can use LINQ:

bool col1HasZero = myDataTable.AsEnumerable().Rows.Select(r => (int)r.[1]).Any(v => v == 0);

and replace Any(predicate) by All(predicate) to see if all the values are zero.

(Link against System.Data.DataSetExtensions.dll for LINQ to DataSets).

Richard