Dataagridview에 내 데이터를 표시하려고합니다. 다른 속성을 가진 클래스를 생성하고 그 목록을 데이터 소스로 사용했습니다. 괜찮 았어. 하지만 중첩 된 클래스가있는 경우에 대비하여 어떻게해야하는지 혼란 스럽습니다. 다음과 같이 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
내 데이터 목록은 다음과 같습니다 : 나는 mycategory를 사용하여 시도
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()
:
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 컨트롤을 바인더 제본 .name하지만 작동하지 않았습니다. 예상 결과를 얻기 위해 할 수있는 일은 무엇입니까? 같은 작업을 수행하기 위해 이것 이외의 다른 좋은 아이디어가 있습니까?
는 의견에 따라 내 질문을 편집 :
나는 U에 의해 주어진 링크를 확인했다. 매우 유용한 n이었습니다. 코드는 C에서 # 이후 나는 VB에서 그것을 변환하려고했습니다. 모든 것이 좋았지 만 대소 문자를 구별하지 못하고 다음 단계로 넘어갔습니다. 중첩 클래스 이름 itemcategory와 속성 이름이 카테고리였습니다. 문제가 발생합니다. 카테고리를 검색하지는 않았지만 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
저는 bindingcontext와 같은 것을 발견했습니다 ...이 컨텍스트에서 유용합니다 – KoolKabin