简体   繁体   English

如何将对象集合绑定到Winforms中的DataGridView

[英]How to bind a collection of objects to a DataGridView in Winforms

If i have two objects, namely Fruit' and Color` and their definitions are as follows: 如果我有两个对象,即Fruit' and Color`,它们的定义如下:

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 (eg List<Fruit>) to a DataGridView? 如何将Fruit的集合(eg List<Fruit>)绑定到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) 而不是像下面的输出:(注意: N.Color的N表示对象Color的命名空间)

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

Update #1: 更新#1:
I found a similar post here on SO and tried some of the suggestion on that post but it's not working... 我发现了一个类似的帖子在这里的SO和尝试了一些关于该职位的建议,但它不工作...

You have multiple options. 你有多种选择。

You can override ToString method in your Color class to return Name like: 您可以在Color类中重写ToString方法以返回Name如:

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

Or instead of assigning List<Fruit> as DataSource you can select a list of anonymous object and select Name of Color in your result like: 或者不是分配List<Fruit>作为DataSource ,你可以选择匿名对象的列表,选择NameColor在你的结果,如:

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

dataGridView1.DataSource = result;

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:

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. 所以我想如果我以某种方式手动将每列放到datagridview,我可以指定我的水果列表中的哪些属性来显示。 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. 但是,在DataGridView列的DataPropertyName上指定类似此Color.Name内容不起作用,并且只会导致一个空白列,而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. 为了使它工作,DataGridView应该有一个单元格格式化函数,以在该给定列中正确显示它的值。 For the complete tutorial on how to do the formatting, you can check it out here from Antonio Bello's blog. 有关如何进行格式化的完整教程,您可以 Antonio Bello的博客中查看。

And that's it, hope it helps you too ^_^ 就是这样,希望它也能帮到你^ _ ^

You can check this property DataGridView.DataSource Property 您可以检查此属性DataGridView.DataSource属性

        // Automatically generate the DataGridView columns.
        dataGridView1.AutoGenerateColumns = true;

        // Set up the data source.
        bindingSource1.DataSource = GetData("Select * From Products");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM