获取一个GridView行的单元格值 [英] Get the cell value of a GridView row
问题描述
我使用的GridView控件 - AutoGenerateSelectButton =真,以便获得第1列单元格的值选择行。
I am using the GridView - AutoGenerateSelectButton="True" to select the row in order to get the Column 1 cell value.
我曾尝试:
GridViewRow row = dgCustomer.SelectedRow;
TextBox1.Text = "Cell Value" + row.Cells[1].Text + "";
和写入了单元格数值,但仅此而已。
And it writes the "Cell Value" but nothing else.
我终于能得到行(指数)的数量,但不单元格的值。
I finally was able to get the number of the row (index) but not the cell value.
GridViewRow row = dgCustomer.SelectedRow;
TextBox1.Text = row.RowIndex.ToString();
我曾尝试:
I have tried:
TextBox1.Text = dgCustomer.Rows[row.RowIndex].Cells[1].Text;
和仍返回索引行。
任何其他建议?
谢谢!
Any other suggestions? Thank you!
推荐答案
试着改变你的代码
// Get the currently selected row using the SelectedRow property.
GridViewRow row = dgCustomer.SelectedRow;
// And you respective cell's value
TextBox1.Text = row.Cells[1].Text
更新:(根据我的意见)
如果所有你正在尝试得到的是所选行的主键值,然后另一种方法是集
UPDATE: (based on my comment) If all what you are trying to get is the primary key value for the selected row then an alternate approach is to set
的DataKeyNames =yourprimarykey
对于可从后面的代码像下面访问的gridview的定义。
for the gridview definition which can be accessed from the code behind like below.
TextBox1.Text = CustomersGridView.SelectedValue.ToString();
这篇关于获取一个GridView行的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!