访问数据表的单元格值 [英] Access cell value of datatable

查看:20
本文介绍了访问数据表的单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮助我如何访问第 4 列中第一个单元格的值?

Can anyone help me how to access for example value of first cell in 4th column?

a b c d
1 2 3 5
g n m l

例如,如果是数据表,如何访问值 d?

for example, how to access to value d, if that would be datatable?

谢谢.

推荐答案

如果您需要对单元格值的弱引用:

If you need a weak reference to the cell value:

object field = d.Rows[0][3]

object field = d.Rows[0].ItemArray[3]

应该这样做

如果您需要强类型引用(在您的情况下是字符串),您可以使用 DataRowExtensions.Field 扩展方法:

If you need a strongly typed reference (string in your case) you can use the DataRowExtensions.Field extension method:

string field = d.Rows[0].Field<string>(3);

(在这种情况下,确保 System.Data 列在命名空间中)

(make sure System.Data is in listed in the namespaces in this case)

索引从 0 开始,所以我们首先访问第一行 (0),然后访问该行的第 4 列 (3)

Indexes are 0 based so we first access the first row (0) and then the 4th column in this row (3)

这篇关于访问数据表的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆