How to assign class property as display data memeber 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/15
5:52 UTC
Read the original article
Hit count: 288
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?
© Stack Overflow or respective owner