获取 GridView 行的单元格值 [英] Get the cell value of a GridView row
问题描述
我正在使用 GridView - AutoGenerateSelectButton = "True"
选择行以获取第 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();
我已经尝试过:
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屋!