绑定到DataGridView,所以每个单元格是绑定的对象而不是每一行 [英] Binding to DataGridView so each cell is a bound object rather than each row

查看:149
本文介绍了绑定到DataGridView,所以每个单元格是绑定的对象而不是每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个DataGridView来代表地图簿中的页面。网格中的每个单元格表示MapGrid对象。用户可以使用它来设置MapGrids的路由。

I'm trying to set up a DataGridView to represent a page out of a map book. Each cell in the grid represents a MapGrid object. Users would use this to set the route for the MapGrids.

public class MapGrid
{
    public int ID { get; set; }
    public short PageNumber { get; set; }
    public char ColumnNumber { get; set; }
    public byte RowNumber { get; set; }
    public RouteSummary Route { get; set; }
}  

public class RouteSummary
{
    public int ID { get; set; }
    public byte RouteNumber { get; set; }
}

我想知道我是否可以使用数据绑定来绑定我的列表MapGrid对象到网格。我想做这样的事情:

I'm wondering if I can use data binding to bind my list of MapGrid objects to the grid. I would like to do something like this:

List<MapGrid> mapGrids = GetMapGrids();
dataGridView.DataSource = mapGrids;

我希望dataGridView显示 MapGrid.RouteSummary.RouteNumber property。

And I would like the dataGridView to display the MapGrid.RouteSummary.RouteNumber property.

我在想,也许我可以创建一个 ILookup< byte,MapGrid> 按行分组MapGrids。那么也许我可以绑定到DataGridView。我不知道如何告诉它显示RouteNumber。

I was thinking maybe I could create an ILookup<byte, MapGrid> to group the MapGrids by row. Then maybe I could bind that to the DataGridView. I'm not sure how I would tell it to display the RouteNumber, though.

如果列数不变,我可以简单地创建一个对象,每个对象的属性为列并将其列表绑定到DataGridView。但事实并非如此简单。列数不是常数。所以我看不到这样可以工作。

If the number of columns was constant I could simply create an object with a property for each column and bind a list of those to the DataGridView. But of course things aren't that simple. The number of columns is not constant. So I don't see how that could work.

任何想法?我试图做超出数据绑定范围的事情吗?现在我想我必须使用我的MapGrid对象手动填充DataGridView,并使用 CellValueChanged 事件来更新内容。

Any ideas? Am I trying to do something beyond the scope of data binding? Right now I'm thinking I'll have to manually populate the DataGridView with my MapGrid objects and use the CellValueChanged event to update stuff.

推荐答案

我得出结论,我想做的是不能通过使用 DataGridView.DataSource 财产。最后,我使用循环来手动添加列和行到 DataGridView

I came to the conclusion that what I wanted to do couldn't be done by using the DataGridView.DataSource property. In the end, I used for loops to manually add columns and rows to the DataGridView.

这篇关于绑定到DataGridView,所以每个单元格是绑定的对象而不是每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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