Asp.net 以编程方式将数据键添加到现有的数据键集
我有一个GridView,它有几个数据键。在一组特定的情况下,我需要在页面加载事件期间从代码隐藏中添加一个额外的datakeyAsp.net 以编程方式将数据键添加到现有的数据键集,asp.net,gridview,datakey,Asp.net,Gridview,Datakey,我有一个GridView,它有几个数据键。在一组特定的情况下,我需要在页面加载事件期间从代码隐藏中添加一个额外的datakey 如何以编程方式将数据键添加到GridView中的现有数据键集?最简单的方法是将DataKeyName的字符串数组转换为ArrayList,添加新的DataKeyName,然后将此ArrayList转换回String()数组,然后使用此设置GridView的DataKeyNames属性。以下是一个例子: Dim arr As New ArrayList() Dim key
如何以编程方式将数据键添加到GridView中的现有数据键集?最简单的方法是将DataKeyName的字符串数组转换为ArrayList,添加新的DataKeyName,然后将此ArrayList转换回String()数组,然后使用此设置GridView的DataKeyNames属性。以下是一个例子:
Dim arr As New ArrayList()
Dim keys As String() = GridView1.DataKeyNames
//Convert to an ArrayList and add the new key.
arr.AddRange(keys)
arr.Add("New Key")
//Convert back to a string array and set the property.
Dim newkeys As String() = CType(arr.ToArray(Type.GetType("System.String")), String())
GridView1.DataKeyNames = newkeys
最简单的方法是将DataKeyName的字符串数组转换为ArrayList,添加新的DataKeyName,然后将此ArrayList转换回String()数组,然后使用此参数设置Gridview的DataKeyNames属性。以下是一个例子:
Dim arr As New ArrayList()
Dim keys As String() = GridView1.DataKeyNames
//Convert to an ArrayList and add the new key.
arr.AddRange(keys)
arr.Add("New Key")
//Convert back to a string array and set the property.
Dim newkeys As String() = CType(arr.ToArray(Type.GetType("System.String")), String())
GridView1.DataKeyNames = newkeys
试试这个
//get length of existing keys
int keyLength = MyGrid.DataKeyNames.Length;
//create newkeys array with an extra space to take the new key
string[] newkeys = new string[keyLength+1];
//copy the old keys to the newkeys array
for (int i = 0; i < keyLength; i++)
newkeys[i] = MyGrid.DataKeyNames[i];
//add the new key in the last location
newkeys[keyLength] = "MyNewKey";
//update your datakeys
MyGrid.DataKeyNames = newkeys;
//获取现有密钥的长度
int keyLength=MyGrid.DataKeyNames.Length;
//创建带有额外空间的newkeys数组以获取新密钥
string[]newkeys=新字符串[keyLength+1];
//将旧密钥复制到newkeys数组
for(int i=0;i
试试这个
//get length of existing keys
int keyLength = MyGrid.DataKeyNames.Length;
//create newkeys array with an extra space to take the new key
string[] newkeys = new string[keyLength+1];
//copy the old keys to the newkeys array
for (int i = 0; i < keyLength; i++)
newkeys[i] = MyGrid.DataKeyNames[i];
//add the new key in the last location
newkeys[keyLength] = "MyNewKey";
//update your datakeys
MyGrid.DataKeyNames = newkeys;
//获取现有密钥的长度
int keyLength=MyGrid.DataKeyNames.Length;
//创建带有额外空间的newkeys数组以获取新密钥
string[]newkeys=新字符串[keyLength+1];
//将旧密钥复制到newkeys数组
for(int i=0;i