访问datagrid视图的元素

时间:2009-04-13 12:14:11

标签: c#

 string s = dataGridView1[0, 0].ToString();
                string s2 = dataGridView1[0, 1].ToString();

我使用上面的方法来访问datagridview的元素。 但是我只能在's'和's2'获得这些值 “DataGridViewTextBoxCell {ColumnIndex = 0,RowIndex = 0}”

“DataGridViewTextBoxCell {ColumnIndex = 0,RowIndex = 1}”

但是datagrid中的值是'1','hello'。 我怎么才能访问它。?

1 个答案:

答案 0 :(得分:3)

string s = dataGridView1[0, 0].Value.ToString();
string s2 = dataGridView1[0, 1].Value.ToString();

您会注意到,当您索引到DGV时,您将获得DataGridViewTextBoxCell类的实例。它会使ToString方法重载,但不会按照您希望/期望的方式重载。

因此,您必须获取此实例中包含的值,然后在其上调用ToString。

相关问题