【问题标题】:Store ID of RowEditing Instead of Index存储 RowEditing 的 ID 而不是索引
【发布时间】:2014-09-27 12:05:59
【问题描述】:

在我的应用程序中,我为用户提供了搜索数据库的选项。当他们单击编辑时,我希望 GridView 通过存储其 ID 仅显示他们正在更新的行。

我正在尝试以下代码,但它没有返回除控件以外的任何内容。

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
     string value = GridView1.Rows[e.NewEditIndex].Cells[3].ToString();
}

关于如何存储我正在寻找的价值的任何想法?

【问题讨论】:

  • 你想得到什么?你的问题不清楚。在哪种情况下,您试图获取值RowUpdating。更多代码将有助于理解问题。
  • 单元格3的值,即编辑被点击的行的行ID。
  • 单元格#3 是声明为BoundField 还是ItemTemplate?
  • 好的,我更新了上面的代码,没有单元格 3 也不是。

标签: c# sql asp.net gridview


【解决方案1】:

使用单元格的 Text 属性

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
     string value = GridView1.Rows[e.NewEditIndex].Cells[3].Text;

     // or 
     string value = GridView1.Rows[e.NewEditIndex].Cells[3].Value;
}

或者,如果您已将控件声明为 <TemplateField> 并尝试获取其值,请尝试

string value = ""
Label lblID = (Label)GridView1.Rows[e.NewEditIndex].FindControl("lblID");
value = lblID.Text;

另一种选择是为 gridview 添加DataKeyNames="ID",您可以使用它来检索

 // assuming that the value of your datakey is numeric value 
 long Id = long.Parse(GridView1.DataKeys[e.NewEditIndex].Values["ID"].ToString());   

请注意,您可以将多个逗号分隔值添加到 DataKeyNames 属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 2019-01-31
    相关资源
    最近更新 更多