将主键保留在Grid中的最佳方法是什么?

时间:2021-07-24 20:09:42

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. I also can use DataKeyNames property of the GridView

有一些方法可以让我这样做,例如我可以获取一个绑定字段,将其Visible属性设置为False并将其绑定到我的主键列。我也可以使用GridView的DataKeyNames属性

But I dont know which one is the best way to keep primary key hidden in the gridview.

但我不知道哪一个是保持主键隐藏在gridview中的最佳方法。

2 个解决方案

#1


2  

You need to use DataKey in gridView.

您需要在gridView中使用DataKey。

DataKeyNames will be the column name of your Primary key.

DataKeyNames将是主键的列名。

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();
}

#2


1  

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" ...

#1


2  

You need to use DataKey in gridView.

您需要在gridView中使用DataKey。

DataKeyNames will be the column name of your Primary key.

DataKeyNames将是主键的列名。

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();
}

#2


1  

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" ...