Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a collection of objects to a DataGridView in Winforms

If i have two objects, namely Fruit' andColor` and their definitions are as follows:

public class Fruit  
{  
  public int FruitId { get; set; }  
  public string Name { get; set; }  
  public Color Color { get; set; }  
}  

public class Color  
{  
  public int ColorId { get; set; }  
  public string Name { get; set; }  
}  

How do I bind a collection of Fruit (e.g. List<Fruit>) to a DataGridView? where the resulting output would be something similar to the following:

+-----+--------+----------+  
| Id  | Name   | Color    |  
+-----+--------+----------+  
| 10  | Apple  | Red      |  
| 20  | Orange | Orange   |  
| 30  | Grapes | Violet   |  
+-----+--------+----------+  

and NOT like the following output below: (Note: the N in N.Color indicates the namespace of the object Color)

+-----+--------+------------+  
| Id  | Name   | Color      |  
+-----+--------+------------+  
| 10  | Apple  | N.Color    |  
| 20  | Orange | N.Color    |  
| 30  | Grapes | N.Color    |  
+-----+--------+------------+  

Update #1:
I found a similar post here on SO and tried some of the suggestion on that post but it's not working...

like image 601
devpro101 Avatar asked Oct 21 '14 16:10

devpro101


People also ask

Which properties are used to bind a DataGridView control?

Binding Data to the Control. For the DataGrid control to work, it should be bound to a data source using the DataSource and DataMember properties at design time or the SetDataBinding method at run time.

What is DataGridView data binding?

The DataGridView control supports the standard Windows Forms data binding model, so it can bind to a variety of data sources. Usually, you bind to a BindingSource that manages the interaction with the data source.

How retrieve data from database and display in DataGridView in C#?

Step 1: Make a database with a table in SQL Server. Step 2: Create a Windows Application and add DataGridView on the Form. Now add a DataGridView control to the form by selecting it from Toolbox and set properties according to your needs.

How do I create a grid in Windows form?

Set global Windows Forms optionsIn Visual Studio, from the Tools menu, select Options. In the left pane of the Options dialog box, click Windows Forms Designer. In the right pane, under the Layout Settings heading, you can set the default grid settings for all the new forms you create.


2 Answers

You have multiple options.

You can override ToString method in your Color class to return Name like:

public class Color
{
    public int ColorId { get; set; }
    public string Name { get; set; }
    public override string ToString()
    {
        return Name;
    }
}  

Or instead of assigningList<Fruit> as DataSource you can select a list of anonymous object and select Name of Color in your result like:

var result = yourListOfFruit
                .Select(r => new
                        {
                            FruitID = r.FruitId, 
                            Name = r.Name, 
                            Color = r.Color.Name,
                        }).ToList();

dataGridView1.DataSource = result;
like image 144
Habib Avatar answered Nov 15 '22 02:11

Habib


Ok, after a couple of days figuring out how to make my app work I managed to find some article that helped me a lot in solving my problem. I thought I'd share it here on SO for you guys so lets start:

First, lets assume we already have a list of fruit stored in a variable fruits, and lets assume we already got it's value from a method:

List<Fruit> fruits = method();  

Now, my problem was... If I bind that list to a datagridview using the following command:

datagridview.DataSource = fruits;  

It will give me a result similar to the following:

+-----+--------+------------+  
| Id  | Name   | Color      |  
+-----+--------+------------+  
| 10  | Apple  | N.Color    |  
| 20  | Orange | N.Color    |  
| 30  | Grapes | N.Color    |  
+-----+--------+------------+   

Which is not what I want. So I thought maybe if I somehow put each columns to the datagridview manually, I could specify which properties from my fruits list to display. So I did something like this:

DataGridViewColumn col3 = new DataGridViewTextBoxColumn();  
col3.DataPropertyName = "Color.Name";  
col3.HeaderText = "Color Name";  
dataGridView1.Columns.Add(col3);  

But then, specifying something like this Color.Name on the DataPropertyName of the DataGridView column is not working, and will only result to a blank column with no data being displayed on the DataGridView. In order for it to work, the DataGridView should have a cell formatting function to properly display it's value in that given column. For the complete tutorial on how to do the formatting, you can check it out here from Antonio Bello's blog.

And that's it, hope it helps you too ^_^

like image 23
devpro101 Avatar answered Nov 15 '22 04:11

devpro101