如何对象的集合结合Winforms中一个DataGridView [英] How to bind a collection of objects to a DataGridView in Winforms

查看:172
本文介绍了如何对象的集合结合Winforms中一个DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个对象,即水果和 Col​​or`及其定义如下:

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; }  
}  



如何绑定的集合水果 (如表<水果GT;) 到一个DataGridView?其中,所产生的输出将类似于下面的内容:

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   |  
+-----+--------+----------+  

和不喜欢下面的输出如下:(注:N的 N.Color 表示物体颜色的命名空间)

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    |  
+-----+--------+------------+  

更新#1:结果
我发现了一个类似的帖子的这里,所以的并尝试了一些关于这个职位的建议,但它不工作...

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

推荐答案

好了,一两天搞清楚如何让我的应用程序下班后我设法找到一些文章,帮助我很多解决我的问题。我想我会在这里分享它所以你们这么让我们开始:

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();  

现在,我的问题是......如果我的列表绑定到使用以下命令一个DataGridView:

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    |  
+-----+--------+------------+   

这是不是我想要的。所以我想也许如果我莫名其妙地把每列到DataGridView手动,我可以指定这从我的水果列表属性显示。所以我做了这样的事情:

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);  



不过,指定这样的事情 Col​​or.Name 上的DataGridView列DataPropertyName不工作,并与在DataGridView显示任何数据只会造成一个空白列。为了为它工作,在DataGridView应该有一个单元格格式设置功能来正确显示它在给定的列值。有关如何做格式化完整的教程,你可以检查出来的安东尼奥·贝洛的博客这里

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 ^_^

这篇关于如何对象的集合结合Winforms中一个DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆