【问题标题】:Properly getting string values from DataRow at specific column从特定列的 DataRow 正确获取字符串值
【发布时间】:2020-07-25 07:34:40
【问题描述】:

我正在尝试从 DataTable 的特定列中的 DataRow 获取值。根据调试器,它应该返回类似“abc12345”的值,而是返回“ListViewSubItem: {abc12345}”。这是为什么呢?

foreach (DataRow row in itemsTable.Rows)
{
    // the required data is in the first column of the DataTable

    // both of the following have been tried:
    string myValue = row[0].ToString();
    string myValue = row.Field<string>(0);
}

【问题讨论】:

  • 你是如何定义列的?您将列类型转换为字符串,因此根据表中的列定义,您将获得不同的响应。
  • @jdweng 就是这样!定义列时忘记设置类型了。

标签: c# .net datatable


【解决方案1】:

你做错了。它应该是 string myValue = row[columnIndex].Text; 并且您正在尝试获取第一列的值。你也可以使用row["columnName"]

注意:我使用了Text 属性。

【讨论】:

    猜你喜欢
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多