Jamil Ferdaus
Jamil Ferdaus

Reputation: 167

DataTable DataRow DataColumn for columns

I am dumping a cvs file into a datatable. I am able to loop through each row and each column. I only do run some some logic for 4 columns out of 16 columns. I tried if but not working. How do i use "for" type syntax? For example, I like to say for columnA do this. For columnB do this. (instead of if(column.ColumnName == "ColumnA") then do something)

Upvotes: 0

Views: 1938

Answers (2)

Hps
Hps

Reputation: 1177

if you already know the name of the columns, then you can always refer to it by the following syntax:

tableObject.Columns[columnName]

and for a particular row:

tableObject.Rows[rowIndex][columnName]

Upvotes: 0

Brad
Brad

Reputation: 15567

I believe you're stuck with testing the column name against some string value. Even if someone comes up with a lambda expression, it's all essentially the same thing: looping and string comparisons.

foreach(DataRow row in table.Rows)
{
    foreach(DataColumn col in table.Columns)
    {
        switch (col.Name)
        {
            case "ColumnA":
                  // do something
                  // if(row[col.Name] = ??) { ... }
                  break;
            case "ColumnB":
                  // do something else
                  break;
        }
    }
}

Upvotes: 1

Related Questions