Simple example of DataGridView..?
-
Hello, I'm hoping someone can help me out here. I want to get started using DataGridView, but I've not actually managed to find any examples that fit what I'm trying to do. What I want to do is very simple: (1) I have a list of class instances thus:
List<MyClass> classList = new List<MyClass>(); classList.Add(new MyClass("Fred")); classList.Add(new MyClass("Wilma")); etc
(2) I want each MyClass member of classList to be represented as a row in my DataGridView (3) I want to show only a subset of the class parameters, for example:public class MyClass { private string name; private string desc; private int anumber; // Want to show these in the interface public String Name { get { return (name); } set { name = value; } } public String Description { get { return (desc); } set { desc = value; } } // etc.. // I DON'T want to show this in the interface public int Number { get { return (anumber); } set { anumber= value; } } }
I understand that I need to set the DataSource parameter of my DataGridView to my data, in some way, but I don't seem to be able to get it to work. Do I need to do something to my classes to get it to work? I think that sums it up. No SQL, nothing just a nice list of data that I want to display. Can anyone help? Many thanks!! :) -
Hello, I'm hoping someone can help me out here. I want to get started using DataGridView, but I've not actually managed to find any examples that fit what I'm trying to do. What I want to do is very simple: (1) I have a list of class instances thus:
List<MyClass> classList = new List<MyClass>(); classList.Add(new MyClass("Fred")); classList.Add(new MyClass("Wilma")); etc
(2) I want each MyClass member of classList to be represented as a row in my DataGridView (3) I want to show only a subset of the class parameters, for example:public class MyClass { private string name; private string desc; private int anumber; // Want to show these in the interface public String Name { get { return (name); } set { name = value; } } public String Description { get { return (desc); } set { desc = value; } } // etc.. // I DON'T want to show this in the interface public int Number { get { return (anumber); } set { anumber= value; } } }
I understand that I need to set the DataSource parameter of my DataGridView to my data, in some way, but I don't seem to be able to get it to work. Do I need to do something to my classes to get it to work? I think that sums it up. No SQL, nothing just a nice list of data that I want to display. Can anyone help? Many thanks!! :)Hi, Use a binding source to bind your list and then bind the binding source to the data grid view. dataGridView1.DataSource = bindingSource1; dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); this.bindingSource1.DataSource = myList; Thanks and Regards Mahen