This is a migrated thread and some comments may be shown as answers.

Dynamic columns and its value

12 Answers 229 Views
GridView
This is a migrated thread and some comments may be shown as answers.
NS
Top achievements
Rank 1
NS asked on 04 Sep 2009, 01:02 PM
Hi,
i have a List containing the following items: (eg with data) (ID, Name, startDate, amount)
1, My Name, 9/1/2009 0:00, 5
2, My Name, 9/1/2009 0:00, -1
3, My Name, 9/1/2009 0:00, 2

The date in the list is referenced from another database table.
What I want to do is bind that data to a grid but dynamically assign possible dates as columns (eg 9/1/2009, 9/8/2009, ...)

 GridViewDataColumn gc;   
            gc = new GridViewDataColumn();  
            gc.UniqueName = "startDatetest1";  
            gc.HeaderText = "9/1/2009";  
            gc.DataMemberPath = "amount";  
            grvCapacity.Columns.Add(gc);  
 
            GridViewDataColumn gc2;  
            gc2 = new GridViewDataColumn();  
            gc2.UniqueName = "startDatetest2";  
            gc2.HeaderText = "9/8/2009";  
            gc2.DataMemberPath = "amount";  
            grvCapacity.Columns.Add(gc2); 

I do this by creating a GridViewDataColumn from codebehind.
The problem I have is with the DataMemberPath of the dynamic columns. It should contain "amount", but it should be the amount for that particular date. How can I achieve this ?

Thanks,
Nicolas

12 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 08 Sep 2009, 09:20 AM
Hi NS,

You should build up the appropriate data source to feed to RadGridView in order to "invert" the grid in such a manner. In other words, you should create a new business object on the fly that has N properties, where N is the number of different dates that you will have, i.e. column names. Or since in Silverlight this would be hard, you can use the Lightweight DataTable from my colleague Vladimir Enchev's blog post. You can dynamically add columns (dates) to this data table and then add the rows (amounts).

I believe that what you are trying to do is called Transpose, and the blog post of my other colleague Stefan Dobrev should get you started in not time. It will help you learn how to transpose your data.

Please, let us know if you have any other questions.

Sincerely yours,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Wayne Bradney
Top achievements
Rank 1
answered on 20 Apr 2010, 03:20 PM
The Lightweight Data Table approach meets an immediate requirement for me (i.e. the ability to dynamically generate columns), but there are a few caveats:

1. It seems that ColumnNames must be valid identifiers (eg I can't have "3Y" as a ColumnName)
2. There's no way (is there?) to have a "Caption" for a column be different from its ColumnName (in order to solve the above)
3. How do I apply formatting to a dynamically generated column eg. show 2 decimals when DataType = typeof(double)?

0
Vlad
Telerik team
answered on 20 Apr 2010, 03:35 PM
Hi,

You can use Header property of the grid columns to define your own header and you can use DataFormatString property to apply formatting.

Sincerely yours,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Wayne Bradney
Top achievements
Rank 1
answered on 20 Apr 2010, 03:57 PM
Not for dynamically generated columns, though, right? I'm not defining these columns in XAML -- where do I set Header and DataFormatString?
0
Vlad
Telerik team
answered on 21 Apr 2010, 07:02 AM
Hello,

To access auto-generated columns you can use AutoGeneratingColumn event.

Sincerely yours,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Wayne Bradney
Top achievements
Rank 1
answered on 21 Apr 2010, 11:59 AM
I see -- that would work. What I was looking for was a way to associate a style or template defined in the XAML, that would apply to a column that was generated automatically -- maybe the template/style would be selected automatically based on the DataType of the column...

An approach like this would be much more appropriate in separated presentation patterns (MVVM in my case), and would keep the Model (which generates the DataTable), entirely separate from the View (which applies the styling), doesn't require any code in the View, and doesn't require the Model to understand GridColumns. The ComponentOne grid control (albeit maybe the WinForms version?) has something like this, if I remember correctly. I assume this isn't currently possible with Telerik?

Anyway, I can live with a single handler method in the View in this case -- thanks.
0
Vlad
Telerik team
answered on 21 Apr 2010, 12:07 PM
Hello,

Our grid is very similar to standard Silverlight DataGrid - if you have any code working with the MS DataGrid I'll gladly help you.

All the best,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Wayne Bradney
Top achievements
Rank 1
answered on 22 Apr 2010, 04:07 PM
Two things about the AutoGeneratingColumn event:

1. It seems to allow me to REPLACE the configuration that would happen normally based on the DataTable.DataColumn, rather that allow me to MODIFY the column configuration, which is what I need in my case. My ViewModel defines the column type and data, and I want to be able to apply column formatting in the View.
2. The event gives me a GridViewColumn, not a GridViewDataColumn, which appears to be a critical difference, since I can't get access to the column's DataType or DataFormatString. Am I missing something?
0
Vlad
Telerik team
answered on 23 Apr 2010, 06:49 AM
Hello,

Once you cast e.Column to GridViewDataColumn you will have access to desired properties.

Kind regards,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Wayne Bradney
Top achievements
Rank 1
answered on 23 Apr 2010, 05:24 PM
OK, I see. I'm almost there. How would I have the cell style vary depending upon the data in that cell? For example, positive values have Foreground="Black", negative values have Foreground="Red" -- is this possible in this dynamic column scenario?
0
Vlad
Telerik team
answered on 26 Apr 2010, 07:42 AM
Hi,

Once you have reference to the column you can set desired column properties no matter if the column is statically declared or dynamically created. My suggestion is to check CellStyleSelector property of GridViewColumn, MSDN documentation related to StyleSelectors and additionally you can check this blog post for more info about the old way for conditional formatting with IValueConverter.

Regards,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Geoff Hardy
Top achievements
Rank 1
answered on 21 May 2010, 04:56 PM
It would be awesome if the RadGridView has a property called "TransposeRowsWithColumns" that would make all of your columns into rows for you.
Tags
GridView
Asked by
NS
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Wayne Bradney
Top achievements
Rank 1
Vlad
Telerik team
Geoff Hardy
Top achievements
Rank 1
Share this question
or