C# 访问数据表的单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9022118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access cell value of datatable
提问by el ninho
Can anyone help me how to access for example value of first cell in 4th column?
任何人都可以帮助我如何访问例如第 4 列中第一个单元格的值?
a b c d
1 2 3 5
g n m l
for example, how to access to value d, if that would be datatable?
例如,如果是数据表,如何访问值 d?
Thanks.
谢谢。
采纳答案by vc 74
If you need a weak reference to the cell value:
如果您需要对单元格值的弱引用:
object field = d.Rows[0][3]
or
或者
object field = d.Rows[0].ItemArray[3]
Should do it
应该做
If you need a strongly typed reference (string in your case) you can use the DataRowExtensions.Fieldextension method:
如果您需要强类型引用(在您的情况下是字符串),您可以使用DataRowExtensions.Field扩展方法:
string field = d.Rows[0].Field<string>(3);
(make sure System.Data is in listed in the namespaces in this case)
(确保 System.Data 在这种情况下列在命名空间中)
Indexes are 0 based so we first access the first row (0) and then the 4th column in this row (3)
索引从 0 开始,因此我们首先访问第一行 (0),然后访问该行中的第 4 列 (3)
回答by gabsferreira
foreach(DataRow row in dt.Rows)
{
string value = row[3].ToString();
}
回答by ashok luhach
string abc= dt.Rows[0]["column name"].ToString();
回答by FrenkyB
You can also try (first cell in 4th column):
您也可以尝试(第 4 列中的第一个单元格):
dt.Rows[0][3]
回答by athena
data d is in row 0 and column 3 for value d :
数据 d 在值 d 的第 0 行和第 3 列中:
DataTable table;
String d = (String)table.Rows[0][3];
回答by Ata Hoseini
public V[] getV(DataTable dtCloned)
{
V[] objV = new V[dtCloned.Rows.Count];
MyClasses mc = new MyClasses();
int i = 0;
int intError = 0;
foreach (DataRow dr in dtCloned.Rows)
{
try
{
V vs = new V();
vs.R = int.Parse(mc.ReplaceChar(dr["r"].ToString()).Trim());
vs.S = Int64.Parse(mc.ReplaceChar(dr["s"].ToString()).Trim());
objV[i] = vs;
i++;
}
catch (Exception ex)
{
//
DataRow row = dtError.NewRow();
row["r"] = dr["r"].ToString();
row["s"] = dr["s"].ToString();
dtError.Rows.Add(row);
intError++;
}
}
return vs;
}

