views:

53

answers:

2

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
A: 

I'd just add a property to item called CategoryName that returns Category.Name and use that.

The Category is part of the Item, but that doesn't mean that you always have to give access to the whole Category to outside classes, just give access to the bits that they need access to at that time as to maximize encapsulation and minimize interdependencies.

Edit: In response to your comment

If you don't want to create the properties as mentioned in my answer above I don't think there is any real solution but you can use the 'CellFormatting' event to sort of get it to work where you set the DataPropertyName to a special identifier and then in the CellFormatting event handler you look up the real value to display. You can find an example of that here, look for tkrasinger's post (or AlexHinton's if you want to use reflection).

ho1
thnx for your suggestion. It was helpful if we had only few properties to give access via item but what if i had alot of properties under that item class like vendor, manufacutrer and other even more... in that case i would like to get some shortcut like exposing the nested class itself
KoolKabin
@KoolKabin: See my amended answer for another suggestion.
ho1
I have tried reading the example. It was in c#. I tried converting it to vb.net. All worked fine but still problem remained.1.) I can't find Active.Utility.EvaluateValue2.) My EvaluateValue is not working fine. if prop name is Category and class name is ItemCategory instead of querying for category it queries for ItemCategory. I have added detail above
KoolKabin
@KoolKabin: I haven't looked in detail, but I think you might have made a mistake on the line where you call `PropA.GetValue`. Unless it's an indexed property you should be sending in `Nothing` as the second parameter. See here for more info: http://msdn.microsoft.com/en-us/library/b05d59ty.aspx
ho1
yeah Ret = EvaluateValue(ObjA, myProp.Substring(myProp.IndexOf(".") + 1)) its passing category instead of mycategory and thus resulting PropA = myObj.GetType().GetProperty(myProp) to nothing in next recursive call...
KoolKabin
A: 
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
Umesh Maharjan