如何在GridView中获取单元格值(无需使用单元格索引) [英] How to get cell value in GridView (WITHOUT using cell index)

查看:117
本文介绍了如何在GridView中获取单元格值(无需使用单元格索引)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从gridview中获取单元格值而不使用单元格索引?
假设我表中的第一列名称是RowNumber。

how to get cell value from gridview without using cell index? Let say the first column name in my table is "RowNumber".

而不是使用

instead of using

string name = GridView1.Rows[0].Cells[0].Text;

类似于

Something like

string name = GridView1.Rows[0].Cells["RowNumber"].Text;


推荐答案

您可以将GridViewRow的DataItem属性复制到DataRowView中,然后引用列名称:

You could cast the GridViewRow's DataItem property into a DataRowView, and then reference the column names:

DataRowView rowView = (DataRowView)GridView1.Rows[0].DataItem;
string name = rowView["RowNumber"].ToString();

您无法从单元格集合,因为它们只是TableCell对象,并且他们不知道底层数据的任何内容。

You can't do this from the Cells collection, because they are just TableCell objects, and they don't know anything about the underlying data.

DataItem属性表示底层数据源中该行的值,所以这就是您想要处理的内容。

The DataItem property represents the values in that row from the underlying datasource, so that's what you want to deal with.

这篇关于如何在GridView中获取单元格值(无需使用单元格索引)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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