2010-03-10 41 views
0

我必須設置GridView.DataKeyNames (這確實是一個關於語法的字符串數組,在VB.Net問題。)如何填充GridView.DataKeyNames,從循環內構建數組? (String數組語法問題)

我從內發現的PrimaryKey的列名循環,即:

For Each fld As IEntityField In Me.llbDataSource.EntityCollection(0).PrimaryKeyFields 
    'fld.Name ' this is where the column name is stored, so I have to get this into an array 
Next 

使用此語法。

GridView.DataKeyNames = new string() { "PowRatHP", "Voltage", "Phases", "Poles", "Drive" } 

因此,基本上,一個人如何聲明一個字符串數組(根據需要由GridView.DataKeyNames),然後從一個循環內填充數組的元素?

回答

2

如果循環的唯一的一點是收集的名稱,只需使用LINQ:

GridView.DataKeyNames = llbDataSource.EntityCollection(0).PrimaryKeyFields.Select(Function(f) f.Name).ToArray() 

或者:

GridView.DataKeyNames = (From f in llbDataSource.EntityCollection(0).PrimaryKeyFields Select f.Name).ToArray() 

或者,只是將它們添加到列表():

Dim l as New List(Of String)() 
For Each fld As IEntityField In Me.llbDataSource.EntityCollection(0).PrimaryKeyFields 
    l.Add(fld.Name) 
Next 
GridView.DataKeyNames = l.ToArray() 

或者,一個陣列:

Dim l(Me.llbDataSource.EntityCollection(0).PrimaryKeyFields.Count - 1) as String 
For i as Integer = 0 to l.Length - 1 
    l(i) = Me.llbDataSource.EntityCollection(0).PrimaryKeyFields(i).Name 
Next 
GridView.DataKeyNames = l 
+0

哇,解決方案的大雜燴,謝謝! – tbone 2010-03-11 00:36:15

0

這是一種方式,BOT不是非常優雅....

Dim dataKeyNames As String = "" 
For Each fld As IEntityField In Me.llbDataSource.EntityCollection(0).PrimaryKeyFields 
    If dataKeyNames <> "" Then dataKeyNames &= "," 
    dataKeyNames &= fld.Name 
Next 
Me.grid.DataKeyNames = dataKeyNames.Split(",".ToCharArray()) 
0

要聲明在VB.net一個數組,使用

Dim stringArray(size) as String 

然後,你可以做任何你需要它。

+0

我不一定知道大小....另外,我不是100%確定,如果這是DataKeyNames期望的數組類型..... – tbone 2010-03-10 21:41:28