获取GridView行的单元格值

我使用GridView – AutoGenerateSelectButton = "True"来选择行以获取Column 1单元格值。

我试过了:

 GridViewRow row = dgCustomer.SelectedRow; TextBox1.Text = "Cell Value" + row.Cells[1].Text + ""; 

它写了“细胞价值”,但没有别的。

我终于能够得到行数(索引)而不是单元格值。

 GridViewRow row = dgCustomer.SelectedRow; TextBox1.Text = row.RowIndex.ToString(); 

我试过了:

 TextBox1.Text = dgCustomer.Rows[row.RowIndex].Cells[1].Text; 

并仍然返回索引行。

还有其他建议吗? 谢谢!

尝试将代码更改为

 // 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 

更新:(基于我的评论)如果您要获取的所有内容都是所选行的主键值,那么另一种方法是设置

datakeynames="yourprimarykey"

对于gridview定义,可以从后面的代码访问,如下所示。

 TextBox1.Text = CustomersGridView.SelectedValue.ToString(); 

Windows窗体迭代技术

 foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Selected) { foreach (DataGridViewCell cell in row.Cells) { int index = cell.ColumnIndex; if (index == 0) { value = cell.Value.ToString(); //do what you want with the value } } } } 

我建议你在模板字段里面使用HiddenField,使用FindControl来查找这个字段。

即:

ASPX

       

代码背后

 protected void gvAttachments_RowDeleting(object sender, GridViewDeleteEventArgs e) { GridView gv1 = (GridView)sender; GridViewRow gvr1 = (GridViewRow)gv1.Rows[e.RowIndex]; //get hidden field value and not directly from the GridviewRow, as that will be null or empty! HiddenField hf1 = (HiddenField)gvr1.FindControl("hfFname"); if (hf1 != null) { .. } } 

你试过Cell[0]吗? 记住索引从0开始,而不是1。

我和你的问题一样。 我发现当我在GridView中使用BoundField标签来显示我的数据时。 row.Cells[1].Text正在从事:

 GridViewRow row = dgCustomer.SelectedRow; TextBox1.Text = "Cell Value" + row.Cells[1].Text + ""; 

但是,当我使用TemplateField标签显示如下数据时:

         

row.Cells[1].Text只返回null。 我很长时间陷入这个问题。 我最近想出来,我想与有同样问题的人分享我的解决方案。 请随时编辑此post和/或更正我。

我的解决方案

 Label lbCod = GridView1.Rows["AnyValidIndex"].Cells["AnyValidIndex"].Controls["AnyValidIndex"] as Label; 

我使用Controls属性来查找我用来显示数据的Label控件,你可以找到你的。 当您找到它并转换为正确的类型对象时,您可以提取文本等。 例如:

 string showText = lbCod.Text; 

参考: 参考

扩展Dennis R上面的答案……这将根据标题文本获得值(因此您不需要知道哪个列…特别是如果它的动态变化)。

在SelectedIndexChange上设置会话变量的示例。

  protected void gvCustomer_SelectedIndexChanged(object sender, EventArgs e) { int iCustomerID = Convert.ToInt32(Library.gvGetVal(gvCustomer, "CustomerID")); Session[SSS.CustomerID] = iCustomerID; } public class Library { public static string gvGetVal(GridView gvGrid, string sHeaderText) { string sRetVal = string.Empty; if (gvGrid.Rows.Count > 0) { if (gvGrid.SelectedRow != null) { GridViewRow row = gvGrid.SelectedRow; int iCol = gvGetColumn(gvGrid, sHeaderText); if (iCol > -1) sRetVal = row.Cells[iCol].Text; } } return sRetVal; } private static int gvGetColumn(GridView gvGrid, string sHeaderText) { int iRetVal = -1; for (int i = 0; i < gvGrid.Columns.Count; i++) { if (gvGrid.Columns[i].HeaderText.ToLower().Trim() == sHeaderText.ToLower().Trim()) { iRetVal = i; } } return iRetVal; } } 
 string id; foreach (GridViewRow rows in grd.Rows) { TextBox lblStrucID = (TextBox)rows.FindControl("grdtext"); id=lblStrucID.Text }