2
votes

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.

2
I'm going to assume data binding just doesn't work like I was hoping. I've decided to abandon the idea and use a TreeView instead of a DataGridView.Ecyrb

2 Answers

2
votes

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.

0
votes

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.