你有一個僞表類和一個僞行類。該行有點通用,沒有強類型的字段。它遵循典型的字典界面將字典綁定到WinForms網格(或組合)?
Dim age As Object = person("Age") 'accessed via default property Item
VS
Dim age As Integer = person.Age 'an actual property typed as an Integer
什麼模式使用,因此,我們可以把我們的僞表和它的行綁定到一個網格或組合框?
Dim rs As New clsResultSet(tblPeople)
Dim id As Object = rs(0)("Id") '913
Dim name As Object = rs(0)("Name") 'Ted
Dim age As Object = rs(0)("Age") '43
Dim occupation As Object = rs(0)("Occupation") 'cab driver
grd.DataSource = rs 'In grid I expect to see Name, Age, Occupation columns
cbo.DataSource = rs
cbo.DisplayMember = "Name" 'could we do this?
cbo.ValueMember = "Id" '...and this?
我讀了所有有關的IList,IBindingList的,BindingSource的,等等,嘗試了一些東西,我仍然令人費解如何得到這個權利。我發現的大多數例子都希望你的記錄對象具有強類型(例如person.Age而不是person(「Age」))。
這裏有一些簡單的類入手:
Public Class clsResultSet 'Like a DataTable
Inherits List(Of clsRecord)
Private mdicFields As New Dictionary(Of String, Object)
Public Sub New(vdt As DataTable) 'Loaded from table
For Each bdc As DataColumn In vdt.Columns
Me.mdicFields.Add(bdc.ColumnName, bdc)
Next
For Each vdr As DataRow In vdt.Rows
Me.Add(New clsRecord(vdr, Me))
Next
End Sub
Public ReadOnly Property Fields As Dictionary(Of String, Object)
Get
Return Me.mdicFields
End Get
End Property
End Class
Public Class clsRecord 'Like a DataRow
Inherits Dictionary(Of String, Object)
Private mrs As clsResultSet
Protected Friend Sub New(vdr As DataRow, vrs As clsResultSet)
Me.mrs = vrs
For Each bPair As KeyValuePair(Of String, Object) In vrs.Fields
Me.Add(bPair.Key, vdr(bPair.Key))
Next
End Sub
End Class
這個問題,如問,得到的回答。因爲我沒有得到我的問題的根源,我reframed it。
你正在使用哪個VB版本? –