如何在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.