DatagGridViewColumn.DataPropertyName to an array element?

Posted by unknown on Stack Overflow See other posts from Stack Overflow or by unknown
Published on 2009-10-01T18:25:41Z Indexed on 2010/03/12 18:27 UTC
Read the original article Hit count: 356

Filed under:
|
|

Hi there,

I'm using a DataGridView binding its datasource to a List, and specifying the properties for each column.

An example would be:

 DataGridViewTextBoxColumn colConcept = new DataGridViewTextBoxColumn();
            DataGridViewCell cell4 = new DataGridViewTextBoxCell();
            colConcept.CellTemplate = cell4;
            colConcept.Name = "concept";
            colConcept.HeaderText = "Concept";
            colConcept.DataPropertyName = "Concept";
            colConcept.Width = 200;
            this.dataGridViewBills.Columns.Add(colConcept);

{... assign other colums...}

And finally

 this.dataGridViewBills.DataSource=billslist; //billslist is List<Bill>

Obviously Class Bill has a Property called Concept, as well as one Property for each column.

Well, now my problem, is that Bill should have and Array/List/whateverdynamicsizecontainer of strings called Years.

Let's assume that every Bill will have the same Years.Count, but this only known at runtime.Thus, I can't specify properties like Bill.FirstYear to obtain Bill.Years[0], Bill.SecondYear to obtain Bills.Years[1]... etc... and bind it to each column.

The idea, is that now I want to have a grid with dynamic number of colums (known at runtime), and each column filled with a string from the Bill.Years List. I can make a loop to add columns to the grid at runtime depending of Bill.Years.Count, but is possible to bind them to each of the strings that the Bill.Years List contains???

I'm not sure if I'm clear enough.

The result ideally would be something like this, for 2 bills on the list, and 3 years for each bill:

--------------------------------------GRID HEADER-------------------------------
NAME      CONCEPT         YEAR1              YEAR2                YEAR3   
--------------------------------------GRID VALUES-------------------------------
Bill1     Bill1.Concept   Bill1.Years[0]     Bill1.Years[1]       Bill1.Years[2]
Bill2     Bill2.Concept   Bill2.Years[0]     Bill2.Years[1]       Bill2.Years[2]


I can always forget the datasource, and write each cell manually, as the MSFlexGrid used to like, but if possible, I would like to use the binding capabilities of the DataGridView.

Any ideas? Thanks a lot.

© Stack Overflow or respective owner

Related posts about c#

Related posts about datagridview