filling a column with property value of object
-
I have two classes like this public myclass1 { public int Id {set;get;) public myclass2 MyClass2{set;get;) } public myclass2 { public int Id {set;get;} public string Name {set;get;} } public list mylist = new List() ; my list has been bonded to datagridview . when displying the collection in the grid , the myclass1 is displaying the object name "DomaninName.myclass2" how to display the propery "Name" of myclass2
-
I have two classes like this public myclass1 { public int Id {set;get;) public myclass2 MyClass2{set;get;) } public myclass2 { public int Id {set;get;} public string Name {set;get;} } public list mylist = new List() ; my list has been bonded to datagridview . when displying the collection in the grid , the myclass1 is displaying the object name "DomaninName.myclass2" how to display the propery "Name" of myclass2
you can overwrite the ToString function
-
I have two classes like this public myclass1 { public int Id {set;get;) public myclass2 MyClass2{set;get;) } public myclass2 { public int Id {set;get;} public string Name {set;get;} } public list mylist = new List() ; my list has been bonded to datagridview . when displying the collection in the grid , the myclass1 is displaying the object name "DomaninName.myclass2" how to display the propery "Name" of myclass2
First, fix the compilation errors in your code: use of ')" where it should be '}', missing
class
keyword on class definitions, missing uppercase onList
, that sort of thing. So us code that works to demonstrate your problem - not a lump of crap your threw together and didn't check at all ... You can display what you want as the cell content, just override ToStrign in your class:public class myclass2 { public int Id { set; get; } public string Name { set; get; } public override string ToString() { return $"{Id}:{Name}"; } }
You can also set the column header text to something more human readable than the property name - just set the HeaderText property of the column:
myDataGridView.DataSource = mylist; myDataGridView.Columns\[1\].HeaderText = "My other class";
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
you can overwrite the ToString function
thank you it works fine for me .