我正在尝试在datagridview中显示我的数据。我创建了一个具有不同属性的类,并使用其列表作为数据源。它工作得很好。但是如果我们有嵌套类,我会感到很困惑。
我的课程如下:
class Category
property UIN as integer
property Name as string
end class
class item
property uin as integer
property name as string
property mycategory as category
end class
我的数据列表如下:
dim myDataList as list(of Item) = new List(of Item)
myDataList.Add(new Item(1,"item1",new category(1,"cat1")))
myDataList.Add(new Item(2,"item2",new category(1,"cat1")))
myDataList.Add(new Item(3,"item3",new category(1,"cat1")))
myDataList.Add(new Item(4,"item4",new category(2,"cat2")))
myDataList.Add(new Item(5,"item5",new category(2,"cat2")))
myDataList.Add(new Item(6,"item6",new category(2,"cat2")))
现在我将datagridview控件绑定为:
DGVMain.AutoGenerateColumns = False
DGVMain.ColumnCount = 3
DGVMain.Columns(0).DataPropertyName = "UIN"
DGVMain.Columns(0).HeaderText = "ID"
DGVMain.Columns(1).DataPropertyName = "Name"
DGVMain.Columns(1).HeaderText = "Name"
DGVMain.Columns(2).DataPropertyName = "" **'here i want my category name**
DGVMain.Columns(2).HeaderText = "category"
DGVMain.datasource = myDataList
DGVMain.refresh()
我尝试过使用mycategory.name但是没有用。可以做些什么来获得预期的结果?除此之外还有更好的想法来完成同样的任务吗?
根据评论编辑我的问题:
我检查了你给出的链接。这很好,非常有用。由于代码在c#中,我试图在vb中转换它。一切顺利,但在区分大小写的情况下失败了,接下来的一个是我有我的嵌套类名itemcategory,我的属性名是category。它引起了这个问题。它没有搜索类别,但搜索了itemcategory。很困惑。我的代码如下:
Private Sub DGVMain_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DGVMain.CellFormatting
Dim DGVMain As DataGridView = CType(sender, DataGridView)
e.Value = EvaluateValue(DGVMain.Rows(e.RowIndex).DataBoundItem, DGVMain.Columns(e.ColumnIndex).DataPropertyName)
End Sub
Private Function EvaluateValue(ByRef myObj As Object, ByRef myProp As String) As String
Dim Ret As String = ""
Dim Props As System.Reflection.PropertyInfo()
Dim PropA As System.Reflection.PropertyInfo
Dim ObjA As Object
If myProp.Contains(".") Then
myProp = myProp.Substring(0, myProp.IndexOf("."))
Props = myObj.GetType().GetProperties()
For Each PropA In Props
ObjA = PropA.GetValue(myObj, New Object() {})
If ObjA.GetType().Name = myProp Then
Ret = EvaluateValue(ObjA, myProp.Substring(myProp.IndexOf(".") + 1))
Exit For
End If
Next
Else
PropA = myObj.GetType().GetProperty(myProp)
Ret = PropA.GetValue(myObj, New Object() {}).ToString()
End If
Return Ret
End Function
答案 0 :(得分:0)
我只需向名为item
的{{1}}添加一个返回CategoryName
并使用该属性的属性。
类别是Item的一部分,但这并不意味着您总是必须将整个Category的访问权限授予外部类,只需访问当时需要访问的位以最大化封装并尽量减少相互依赖。
编辑:回复您的评论
如果您不想创建我在上面的答案中提到的属性,我认为没有任何真正的解决方案,但您可以使用'CellFormatting'事件进行排序,让它在您设置{的地方工作{1}}到特殊标识符,然后在Category.Name
事件处理程序中查找要显示的实际值。您可以找到here的示例,查找tkrasinger的帖子(如果您想使用反射,则查找AlexHinton的帖子)。
答案 1 :(得分:0)
Public Class Category
Dim uni As Integer
Dim name As String
Public Sub New(ByVal i As Integer, ByVal n As String)
Me.UIN = i
Me.name = n
End Sub
Public Sub New()
End Sub
Property UIN() As Integer
Get
Return uni
End Get
Set(ByVal value As Integer)
uni = value
End Set
End Property
Property Names() As String
Get
Return Me.name
End Get
Set(ByVal value As String)
Me.name = value
End Set
End Property
**Public Overrides Function ToString() As String
Return name.ToString()
End Function**
End Class
Public Class item
Dim uni As Integer
Dim name As String
Dim category As New Category()
Property UIN() As Integer
Get
Return uni
End Get
Set(ByVal value As Integer)
uni = value
End Set
End Property
Property Names() As String
Get
Return Me.name
End Get
Set(ByVal value As String)
Me.name = value
End Set
End Property
Property mycategory() As Category
Get
Return Me.category
End Get
Set(ByVal value As Category)
Me.category = value
End Set
End Property
Public Sub New(ByVal i As Integer, ByVal nm As String, ByVal ct As Category)
Me.UIN = i
Me.Names = nm
Me.mycategory = ct
End Sub
End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myDataList As List(Of item) = New List(Of item)
myDataList.Add(New item(1, "item1", New Category(1, "cat1")))
myDataList.Add(New item(2, "item2", New Category(1, "cat1")))
myDataList.Add(New item(3, "item3", New Category(1, "cat1")))
myDataList.Add(New item(4, "item4", New Category(2, "cat2")))
myDataList.Add(New item(5, "item5", New Category(2, "cat2")))
myDataList.Add(New item(6, "item6", New Category(2, "cat2")))
DGVMain.AutoGenerateColumns = False
DGVMain.ColumnCount = 3
DGVMain.Columns(0).DataPropertyName = "UIN"
DGVMain.Columns(0).HeaderText = "ID"
DGVMain.Columns(1).DataPropertyName = "Names"
DGVMain.Columns(1).HeaderText = "Name"
**DGVMain.Columns(2).DataPropertyName = "mycategory"**
DGVMain.Columns(2).HeaderText = "Category"
DGVMain.datasource = myDataList
DGVMain.refresh()
End Sub