none
DataGridView - Binding RRS feed

  • Question

  • Hi,

    I am trying to get my head around how to bind with dataGridView.

    I have a custom collection,

    public class Vehicle
    {
            public string Name {get; set;}
            public Category Type {get; set; }
    }
    
    public class Category
    {
              public string Name {get; set;}
              public List<Specifications> Specifications {get; set;}
    }
    
    public class Specifications
    {
             public string Name {get; set;}
             public string Type {get; set;}
             public string Value {get; set;}
    }
    
    

     

    // Business Logic returns List<Vehicles> (lets call this myVehicleList)
    
    

    To perform the binding operation i am doing the following,

    dataGridView1.DataSource = myVehicleList;
    dataGridView1.AutoGenerateColumns = true;
    
    

    This does not work! On the windows form I end up with one cell. What am i missing? Creating a datatable is too much work and why would that be required when i already have a collection of public properties that i am trying to bind with.

    Any help appreciated.

    Cheers, Tarun


    Please remember to mark the replies as answers if they help.

    Tarun Arora

    Blog: http://geekswithblogs.net/TarunArora  Subscribe in a reader

    Monday, September 12, 2011 8:15 PM

Answers

  • It should be having a BindingSource between the UI and Business Object DataSource.

    Like this:

            BindingSource bindingSource;
            private void Form1_Load(object sender, EventArgs e)
            {
                bindingSource = new BindingSource();
                bindingSource.DataSource = typeof(Vehicle);
    
                Category category0 = new Category();
                category0.Name = "category0";
                category0.Specifications = new List<Specifications> 
                {
                    new Specifications(){ Name = "specifications0_Name", Type = "specifications0_Type", Value = "specifications0_Value"},
                    new Specifications(){ Name = "specifications1_Name", Type = "specifications1_Type", Value = "specifications1_Value"},
                    new Specifications(){ Name = "specifications2_Name", Type = "specifications2_Type", Value = "specifications2_Value"},
                    new Specifications(){ Name = "specifications3_Name", Type = "specifications3_Type", Value = "specifications3_Value"},
                };
    
                Vehicle vehicle0 = new Vehicle();
                vehicle0.Name = "vehicle0";
                vehicle0.Type = category0;
    
                bindingSource.Add(vehicle0);
    
    
                this.dataGridView1.DataSource = bindingSource;
                
            }
    

    If you want to let it generate the column automatically, you will be need to override a ToString method for the Category class, since when you do nothing for its instance when it displayed in the cell, it will call the ToString method and display the returned string in the cell for you.

    For more Busniess object data source in control binding programming, please read those document like:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx

    http://msdn.microsoft.com/en-us/library/ms951295.aspx

    There's several "How to" and "Walkthrough" documents/samples for you to learn the Busniess object data source binding.


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 13, 2011 8:32 AM
    Moderator
  • Tarun -- a slight variation on your code to use a BindingSource (different than the what Mike posted) should do the trick:

    BindingSource bsVehicles;
    
    public void GetData()
    {
         var vehicles = GetVehicles();
         bsVehicles = new BindingSource();
         bsVehicles.DataSource = vehicles;
         dataGridView1.DataSource = bsVehicles;
         dataGridView1.AutoGenerateColumns = true;
    }
    
    

     


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, September 17, 2011 5:08 PM

All replies

  • Hi

    u're declaring your list with this way??

     

    Dim list As New List(Of Vehicle)
    dataGridView1.DataSource = list
    



    Best Regards...Please mark as answer if my post is helpful
    Tuesday, September 13, 2011 7:30 AM
  • Hi YosrJ,
    The list is being instanciated, please refer to the snippet below,
     
     
    public void GetData()
    {
         var vehicles = GetVehicles();
         dataGridView1.DataSource = vehicles;
         dataGridView1.AutoGenerateColumns = true;
    }
    
    
    private List<Vehicle> GetVehicles()
    {
            var vehicles = new List<Vehicle>();
    
            ...
    
            return vehicles
    }
    
    

    HTH

    Please remember to mark the replies as answers if they help.

    Tarun Arora

    Blog: http://geekswithblogs.net/TarunArora  Subscribe in a reader

    Tuesday, September 13, 2011 8:18 AM
  • It should be having a BindingSource between the UI and Business Object DataSource.

    Like this:

            BindingSource bindingSource;
            private void Form1_Load(object sender, EventArgs e)
            {
                bindingSource = new BindingSource();
                bindingSource.DataSource = typeof(Vehicle);
    
                Category category0 = new Category();
                category0.Name = "category0";
                category0.Specifications = new List<Specifications> 
                {
                    new Specifications(){ Name = "specifications0_Name", Type = "specifications0_Type", Value = "specifications0_Value"},
                    new Specifications(){ Name = "specifications1_Name", Type = "specifications1_Type", Value = "specifications1_Value"},
                    new Specifications(){ Name = "specifications2_Name", Type = "specifications2_Type", Value = "specifications2_Value"},
                    new Specifications(){ Name = "specifications3_Name", Type = "specifications3_Type", Value = "specifications3_Value"},
                };
    
                Vehicle vehicle0 = new Vehicle();
                vehicle0.Name = "vehicle0";
                vehicle0.Type = category0;
    
                bindingSource.Add(vehicle0);
    
    
                this.dataGridView1.DataSource = bindingSource;
                
            }
    

    If you want to let it generate the column automatically, you will be need to override a ToString method for the Category class, since when you do nothing for its instance when it displayed in the cell, it will call the ToString method and display the returned string in the cell for you.

    For more Busniess object data source in control binding programming, please read those document like:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx

    http://msdn.microsoft.com/en-us/library/ms951295.aspx

    There's several "How to" and "Walkthrough" documents/samples for you to learn the Busniess object data source binding.


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 13, 2011 8:32 AM
    Moderator
  • Mike,

    Thanks for your reply, please keep the post open, i will try the recommendation and confirm by tomorrow morning.

    Cheers, Tarun


    Please remember to mark the replies as answers if they help.

    Tarun Arora

    Blog: http://geekswithblogs.net/TarunArora  Subscribe in a reader

    Tuesday, September 13, 2011 8:56 AM
  • change this  var = new List<Vehicle>();

     

    by

     List<Vehicle> vehicles = new List<Vehicle>();
     vehicles.Add("your items");
     ....
      return vehicles;
    }
    


     



    Best Regards...Please mark as answer if my post is helpful
    Tuesday, September 13, 2011 9:03 AM
  • Ok, and I believe my reply contains the simple demonstration and two MSDN document help you deep into this aspect, which will let you learn it clearly, and then you can go on do the remain work.

     

    Have a good day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, September 14, 2011 3:32 AM
    Moderator
  • Tarun -- a slight variation on your code to use a BindingSource (different than the what Mike posted) should do the trick:

    BindingSource bsVehicles;
    
    public void GetData()
    {
         var vehicles = GetVehicles();
         bsVehicles = new BindingSource();
         bsVehicles.DataSource = vehicles;
         dataGridView1.DataSource = bsVehicles;
         dataGridView1.AutoGenerateColumns = true;
    }
    
    

     


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, September 17, 2011 5:08 PM
  • Thank you Bonnie and Mike, Vote + 1, both the solutions work well for me. 
    Please remember to mark the replies as answers if they help.

    Tarun Arora

    Blog: http://geekswithblogs.net/TarunArora  Subscribe in a reader

    Sunday, September 18, 2011 12:38 PM
  • You're welcome, Tarun!  I'm glad I could help!  =0)
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Sunday, September 18, 2011 3:25 PM
  • You're welcome!

     

    Have a good day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, September 19, 2011 2:19 AM
    Moderator