How to assign class property as display data member in datagridview

Posted by KoolKabin on Stack Overflow See other posts from Stack Overflow or by KoolKabin
Published on 2010-06-15T05:45:57Z Indexed on 2010/06/16 5:42 UTC
Read the original article Hit count: 279

Filed under:
|
|
|

hi guys,

I am trying to display my data in datagridview. I created a class with different property and used its list as the datasource. it worked fine. but I got confused how to do that in case we have nested class.

My Classes are as follows:

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

my data list as follows:

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")))

Now I binded the datagridview control like:

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()

I have tried using mycategory.name but it didn't worked. What can be done to get expected result? Is there any better idea other than this to accomplish the same task?

Edited My question as per comment:

I have checked the link given by u. It was nice n very usefull. Since the code was in c# i tried to convert it in vb. Everything went good but failed at a point of case sensitive and next one is that i had my nested class name itemcategory and my property name was category. there it arouse the problem. it didn't searched for category but it searched for itemcategory. so confused on it. My Code as follows:

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

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about class