简体   繁体   English

在网格中保留主键的最佳方法是什么

[英]What is the best way to keep Primary key in Grid

I need to keep primary key in grid so that when I need to update a certain row, I can retrieve the latest data based on the primary key, then display it in fields, and then when user changes the fields values, the data can be updated. 我需要将主键保留在网格中,以便在需要更新特定行时,可以基于主键检索最新数据,然后在字段中显示它,然后当用户更改字段值时,数据可以是更新。

There are some methods that allow me to do that, For example I can take a bound field, set its Visible property to False and bind it to my Primary key column. 有一些方法可以使我做到这一点,例如,我可以获取一个绑定字段,将其Visible属性设置为False并将其绑定到我的主键列。 I also can use DataKeyNames property of the GridView 我也可以使用GridView的DataKeyNames属性

But I dont know which one is the best way to keep primary key hidden in the gridview. 但是我不知道哪种方法是将主键隐藏在gridview中的最佳方法。

You need to use DataKey in gridView. 您需要在gridView中使用DataKey

DataKeyNames will be the column name of your Primary key . DataKeyNames将是您的Primary key的列名称。

Aspx Code (in gridview starting tag): Aspx代码(在gridview的开始标记中):

DataKeyNames = "Customer_ID"

C# Code (to access DataKey): C#代码(用于访问DataKey):

foreach (GridViewRow row in gvCustomers.Rows)
{
    string Customer_ID= gvCustomers.DataKeys[row.RowIndex]["Customer_ID"].ToString();
}

Yes you are correct, you can use GridView.DataKeyNames Property 是的,您是正确的,可以使用GridView.DataKeyNames属性

Gets or sets an array that contains the names of the primary key fields for the items displayed in a GridView control. 获取或设置一个数组,该数组包含GridView控件中显示的项目的主键字段的名称。

Something like this: 像这样:

<asp:GridView DataKeyNames = "myID" ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM