Question about custom databinding second-level object to datagridview

Hello there, /r/csharp!

I'm currently writing my hobby project, and can't wrap my head around DataBinding for second-level properties. Here's a sample code:

using System; using System.Collections; using System.Collections.Generic; public class Program { public static void Main() { Console.WriteLine("Hello World"); List<TaskObject> taskList = new List<TaskObject>() { new TaskObject(){ GUID = new Guid(), Id = 1, Parameters = new Hashtable() { {"param1","val1"}, {"param2","val2"} } }, new TaskObject(){ GUID = new Guid(), Id=2, Parameters = new Hashtable() { {"param1","val3"}, {"param2","val4"} } }, }; } } class TaskObject{ public Guid GUID {get;set;} public Hashtable Parameters {get;set;} public int Id {get;set;} } 

I would like to find a way to get Tasklist databinded to DataGridView so I could see all of parameters of all tasks in grid-like manner, one task per row, with all of it's properties values as columns. Like:

Id Param1 Param2
1 val1 val2
2 val3 val4

Problem is that TaskObject can have as many Parameters as user wants (all of TaskObject will have the same count of Parameters, though) loaded from Excel. Excel looks like

Param1 Param2
val1 val2
val3 val4

I'm not so familiar with WPF, so plain old WinForms for now 🙁

The only choices I can imagine:

  1. Naively add rows by hand (made it work for now). But I honestly don't like to do it such way, because I'm losing all of the nice features of DataBinding, like auto-updating datagridview when value of some property changes.
  2. Refactor Tasklist to one large class, containing one array of "ColumnNames", and second two-dimensional array of "ParameterValues". Again, I'm not so happy with such solution, because then I'm again losing control of single Task.

Maybe, someone more experienced could give me some hints about how should I approach to this task differently?

P.S. I've seen an article about second-level binding on MSDNTS, but if I understood it correctly – it doesn't help in my situation, because I don't know number\names of properties at design-time, they're loaded from excel.

by soralex via /r/csharp

Leave a Reply