如何从gridview中检索选中行的主键

  • 本文关键字:检索 gridview asp.net oracle10g
  • 更新时间 :
  • 英文 :


您能告诉我如何检索网格视图中选中行的主键吗?我不想显示那个值什么的,只是用它来修改那个特定的行(删除,改变列值等)我在后端使用Oracle 10g。谢谢。

使用DataKeys的gridview
详细信息:http://msdn.microsoft.com/en-in/library/system.web.ui.webcontrols.gridview.datakeys.aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  
    DataKeyNames="emp_id">

DataKeyNames将主键值放在该字段中。
细节链接
http://www.codeproject.com/Articles/23833/DataKeyNames

In Aspx.Page…

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False"  
    DataKeyNames="emp_id">
在Cs

。页面,点击事件

for(int i=0;i<gv.rows.count;i++)
{
    GridviewRow row=gv.rows[i];
    cheakbox c1=(Cheakbox)row.FindControl("chkId");
    if(c1.cheaked)
    {
         int id=Convert.toInt32(gv.Datakeys[i]);
    }
}

试试这个

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ProductId" AllowPaging="True" 
        BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" 
        CellPadding="2" ForeColor="Black" GridLines="None" 
        onpageindexchanging="GridView1_PageIndexChanging" 
        onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 
        onrowcancelingedit="GridView1_RowCancelingEdit" 
        onrowdeleting="GridView1_RowDeleting" Width="603px">
  </asp:GridView>

现在在你的代码后面例如update

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int productid=int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
}

最新更新