C# 从特定列的DataRow中正确获取字符串值

C# 从特定列的DataRow中正确获取字符串值,c#,.net,datatable,C#,.net,Datatable,我试图从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 tri

我试图从
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);
}
foreach(itemsTable.Rows中的DataRow行)
{
//所需数据位于DataTable的第一列中
//以下两项都已尝试过:
字符串myValue=行[0]。ToString();
字符串myValue=行字段(0);
}

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


注意:我使用了
Text
属性。

您是如何定义该列的?您正在将列类型强制转换为字符串,因此根据表中的列定义,您将得到不同的响应。@jdweng就是这样!我在定义列时忘记设置类型。