DataTable DataRow DataColumn 列

发布于 2024-10-03 00:49:43 字数 168 浏览 0 评论 0原文

我正在将 cvs 文件转储到数据表中。我能够循环遍历每一行和每一列。我只对 16 列中的 4 列运行一些逻辑。我尝试过但不工作。如何使用“for”类型语法?例如,我喜欢说对于columnA 这样做。对于 B 列,执行此操作。 (而不是 if(column.ColumnName == "ColumnA") 然后做一些事情)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛夏尉蓝 2024-10-10 00:49:43

我相信您一直在根据某些字符串值测试列名称。即使有人提出了 lambda 表达式,它本质上都是一样的:循环和字符串比较。

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

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;
        }
    }
}
╰つ倒转 2024-10-10 00:49:43

如果您已经知道列的名称,那么您始终可以通过以下语法引用它:

tableObject.Columns[columnName]

对于特定行:

tableObject.Rows[rowIndex][columnName]

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文