views:

394

answers:

2

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

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;

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

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.

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.

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.

A: 

Why bother with the extra class? May as well have your MapGrid defined as

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

You can then use simple binding as you desire.

AdamRalph
The RouteSummary class is actually used elsewhere in the code. So it's good OO design to break it out. But, let's say I do go with a single class like you suggested. How would that help me bind as I was describing? I don't follow.
Ecyrb
what's the relationship between your objects? is it a one to many between RouteNumber and MapGrid? It's unclear exactly how you want your output to look. I thought the problem was that you were having difficulty displaying the route number on each map grid row, because it was a property of a RouteSummary object field rather than a property of the MapGrid object.
AdamRalph
Yes, one `Route` can have many `MapGrids`. The output I'm shooting for is, for example, a `DataGridView` with columns A-F and rows 1-6 (a 6x6 grid, but these numbers are variable, which is what makes it difficult). I was hoping to set the `DataGridView.DataSource` such that if a `MapGrid` has `ColumnNumber` = 'A' and `RowNumber` = 1 the corresponding cell in the `DataGridView` would display the `MapGrid.Route.RouteNumber`. Sorry for the delayed response, I didn't notice you replied until just now.
Ecyrb
A: 

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.

Ecyrb