Click here to Skip to main content
15,877,384 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to bind a colletion to datagriview.
XML
private void loadGridview(ObservableCollection<cLabTest> list)
     {

         Gridview.DataSource = list;

     }

i surely that i did call my function while i debugged.
But it did not works.
I based this article: http://tech.pro/tutorial/776/csharp-tutorial-binding-a-datagridview-to-a-collection[^].
it said:
"Binding this to a DataGridView is painfully easy. Simply set the DataSource property of the DataGridView to the List" and "the DataGridView will automatically create columns for each property in the Car object".

I am newbie, i read some article but they are too high for me.
Could someone tell me why please.
Posted
Updated 25-Mar-15 4:07am
v2
Comments
Herman<T>.Instance 25-Mar-15 10:14am    
what in your code stops you from this task?
BillWoodruff 25-Mar-15 11:15am    
How does it "not work" ? Any error messages ?

Verify the autogeneratecolumns property value. Default it should be true in this scenario
 
Share this answer
 
Comments
Tokisan 25-Mar-15 11:35am    
i set it
private void loadGridview(ObservableCollection<clabtest> list)
{

Gridview.AutoGenerateColumns = true;

Gridview.DataSource = list;

}
but has no error, the debugger say noting and it still does not work out.
Tokisan 25-Mar-15 23:46pm    
Datagridview of Winfrom does not support ObservableCollection.
I read it on https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx
The reason ,my code did not worked out , that is i forgot to write the method {set; get;} for class's properties
Recently, i noticed the debug still show out that my object have values event i didn't declare {set;get;};
Thank 'Rajesh Varma Buddaraju' and everyone for helping me.
Thank a lots.
 
Share this answer
 
I tried the sample, It is working fine for me.

XML
private void Form1_Load(object sender, EventArgs e)
        {
            ObservableCollection<Car> cars = new ObservableCollection<Car>();
            cars.Add(new Car("Ford", "Mustang", 1967));
            cars.Add(new Car("Shelby AC", "Cobra", 1965));
            cars.Add(new Car("Chevrolet", "Corvette Sting Ray", 1965));
            loadGridview(cars);
        }
        private void loadGridview(ObservableCollection<Car> list)
        {
            dgGrid.DataSource = list;
        }

C#
public class Car
   {
       private string _make;
       private string _model;
       private int _year;

       public Car(string make, string model, int year)
       {
           _make = make;
           _model = model;
           _year = year;
       }

       public string Make
       {
           get { return _make; }
           set { _make = value; }
       }

       public string Model
       {
           get { return _model; }
           set { _model = value; }
       }

       public int Year
       {
           get { return _year; }
           set { _year = value; }
       }
   }




I placed the entire code for you. Try it once.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900