2012-08-11 71 views

回答

3

最简单的方法是将DataKeyNames的String数组转换为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 
3

试试这个

//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; 
相关问题