开发者

DataTable DataRow DataColumn for columns

I am dumping a cvs file into a datatable. I am able to loop through each row and each co开发者_运维知识库lumn. 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)


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;
        }
    }
}


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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜