2010-06-15 9 views
1

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 
+0

저는 bindingcontext와 같은 것을 발견했습니다 ...이 컨텍스트에서 유용합니다 – KoolKabin

답변

0

난 그냥 item에 속성을 추가 거라고는 Category.Name를 반환하고 그 사용 CategoryName을했다.

카테고리는 항목의 일부이지만, 외부 클래스에 전체 카테고리에 대한 액세스 권한을 항상 부여해야한다는 것을 의미하지는 않습니다. 그 때 액세스 할 필요가있는 비트에 액세스 권한을 부여하면됩니다. 캡슐화하고 상호 의존성을 최소화합니다.

편집 : 귀하의 코멘트에 대한 응답으로

내가 거기에 실제 솔루션입니다하지만 당신은 'CellFormatting'이벤트를 사용할 수 있다고 생각하지 않습니다 위의 내 대답에 언급 된대로 속성을 만들지 않으려면 당신이 DataPropertyName을 특수 식별자로 설정하고 CellFormatting 이벤트 핸들러에서 표시 할 실제 값을 찾으려면 작업을 정렬 시키십시오. here의 예를 찾을 수 있습니다. tkrasinger의 게시물 (또는 리플렉션을 사용하려는 경우 AlexHinton)을 찾으십시오.

+0

thnx 제안을 위해. 항목을 통해 액세스 권한을 부여하는 속성이 거의없는 경우 도움이되었습니다.하지만 공급 업체, manufacutrer 및 그 밖의 다른 항목과 같은 항목 클래스 아래에 속성이 많이있는 경우에는 어떻게 될까요?이 경우에는 해당 항목을 노출하는 것과 같은 바로 가기를 얻고 싶습니다. 중첩 클래스 자체 – KoolKabin

+0

@ KoolKabin : 다른 제안을 보려면 수정 된 답변을 참조하십시오. –

+0

예제를 읽으려고했습니다. 그것은 C#에있었습니다. 나는 그것을 vb.net로 변환하려고 시도했다. 모두 잘 작동했지만 여전히 문제가 남아 있습니다. 1.) Active.Utility.EvaluateValue를 찾을 수 없습니다. 2.) 내 EvaluateValue가 제대로 작동하지 않습니다. 소품 이름이 Category이고 클래스 이름이 Category를 쿼리하는 대신 ItemCategory 인 경우 ItemCategory를 쿼리합니다. – KoolKabin

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 
관련 문제