Jui Test
Jui Test

Reputation: 2409

How to check specific column exist in datarow?

I came across situation,where columnname for datatable is dynamic.While fetching a data I want to check existence of column.

DataTable table = ds.Table["Sample1"]

if(table.Row.Count > 0)
{
foreach(DataRow dr in table.Rows )
{
    if(dr.Table.Column.Contain("DateInfo"))
    {
        // store value in variable
        // first approach
    }

    if(table.Column.Contain("DateInfo"))
    {
        // store value in variable
        // second approach
    }
}
}

Which one is best approach?

Upvotes: 2

Views: 6661

Answers (2)

Vishal Suthar
Vishal Suthar

Reputation: 17183

Will this be enough:

1st Approach: Which will simply check in an entire DataTable.

datatable.Columns.Contains("column")

2nd Approach: which will check for each row collection in DataTable

dr.Table.Columns.Contains("column")

3rd Approach: Which fetch each columns in DataColumnCollection object and then check if it contains the specific field or not.

DataColumnCollection columns = datatable.Columns;

if (columns.Contains(columnName))

So these all approaches are better in their own way. you can use whatever you find it better.

Upvotes: 3

senthilkumar2185
senthilkumar2185

Reputation: 2566

This is best one

dr.Table.Column.Contain("DateInfo")

foreach loop get single row at a time sometimes if any conditional is possible in this method

Upvotes: 1

Related Questions