字典绑定到一个WinForms网格(或组合)? [英] Binding a dictionary to a WinForms grid (or combo)?
问题描述
您有一个伪表类和伪行级。该行是有些通用的,不具有强类型字段。它遵循典型的字典界面
You have a pseudo-table class and a pseudo-row class. The row is somewhat generic and has no strongly-typed fields. It follows the typical dictionary interface
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而不是人(时代))。
I read all about IList, IBindingList, BindingSource, etc., tried some things, and I'm still puzzling over how to get this right. Most examples I've found expect your record objects to be strongly typed (e.g. person.Age rather than person("Age")).
下面是一些简单的类入手:
Here are some simple classes to start with:
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它的,因为我没有得到我的问题的根源。
This question, as asked, was answered. I reframed it since I didn't get to the root of my problem.
推荐答案
据我所知是没有办法直接绑定到字典对象。这里有一个快速的解决方法与固定数据源的组合框。创建一个新的列表,通过选择和匿名对象所需要的属性(适用于Visual Studio 2008和更新版本):
As far as I know there is no way to directly bind to dictionary objects. Here's a quick workaround for a combobox with a fixed DataSource. Create a new list with the desired properties using Select and anonymous objects (works with Visual Studio 2008 and newer):
Dim rs As New clsResultSet(tblPeople)
cbo.DataSource = rs.Select(Function(x) New With {.Name = x("Name"), .Id = x("Id")}).ToList()
cbo.DisplayMember = "Name"
cbo.ValueMember = "Id"
我想你可以使用类似的方法绑定到一个DataGridView,但我会建议使用强类型类,而不是(Person类具有属性的姓名,年龄等),除非它是一个读 唯一的DataGridView不会改变。
I suppose you could use a similar approach to bind to a DataGridView, but I would recommend using a strongly-typed class instead (a Person class with properties Name, Age, etc.), unless it's a read- only DataGridView that won't change.
编辑:
您可能会感兴趣的 DataGridView.DataSource 文档。基本数据源可以是一个的IList
, IListSource
, IBindingList的
或 IBindingListView
。数据表和数据集类实现 IListSource
所以它可能是值得尝试,以实现该接口为你自己的类。
You might be interested in the Remarks section of the DataGridView.DataSource documentation. Basically the DataSource can be either an IList
, IListSource
, IBindingList
or IBindingListView
. DataTable and DataSet classes implement IListSource
so it might be worth trying to implement this interface for your own class.
这篇关于字典绑定到一个WinForms网格(或组合)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!