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

Variable number of columns in the Grid

9 Answers 562 Views
Grid for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Yuri
Top achievements
Rank 1
Yuri asked on 08 Mar 2013, 09:48 PM
I have taken a look at your examples, and it does not seem that it's possible to create a grid with variable number of columns. My use case is as such:
I want to have a grid, but depending on the data coming in (date columns, data points rows), the number of columns could vary per page. Your current example uses binding to a specific class, which has a constant set of properties. In my case I want to be able to create and specify the number of columns in the code. Is it possible to do that?

9 Answers, 1 is accepted

Sort by
0
Ivaylo Gergov
Telerik team
answered on 13 Mar 2013, 05:32 PM
Hello Yuri,

Thank you for your interest.

It is possible to add columns manually through the RadDataGrid.Columns property and bind each one to a different set of data (you will also need to set RadDataGrid.AutoGenerateColumns = false). If this is not useful to you - can you provide us with more detailed information about what type of data you receive and what is the desired result?

I am looking forward to your reply.

 

Greetings,
Ivaylo Gergov
the Telerik team
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
0
Yuri
Top achievements
Rank 1
answered on 19 Mar 2013, 03:08 PM
Ok let me elaborate:
I want to create a class that will dynamically determine the number of columns that are needed to be displayed, depending on the data coming in (See the screenshot attached). This particular dataset has 6 data points (marked by dates as column headings). However, if only 5 dates were returned, I would end up with an empty column on the right hand side, because the class I am binding the grid to looks like this:
public class ResultDisplay6 : ResultDisplayTitle
    {
        public String resultValue0 { get; set; }
        public String resultValue1 { get; set; }
        public String resultValue2 { get; set; }
        public String resultValue3 { get; set; }
        public String resultValue4 { get; set; }
        public String resultValue5 { get; set; }
    }
This class is responsible for displaying 1 row of the grid.
So if I have information coming in with 5 dates only, I bind it instead to the class:
public class ResultDisplay5 : ResultDisplayTitle
{
    public String resultValue0 { get; set; }
    public String resultValue1 { get; set; }
    public String resultValue2 { get; set; }
    public String resultValue3 { get; set; }
    public String resultValue4 { get; set; }
}

It would be much easier if I could just bind it instead to a class with a List<> inside and the grid would determine the necessary amount of columns to display. Is there a better approach to this?
0
Ivaylo Gergov
Telerik team
answered on 21 Mar 2013, 06:31 PM
Hi Yuri,

Thank you for your interest.

In this case you can take advantage of the GenerateColumn command where you can put some custom logic to check if there is a returned value for the current property. Thus, if there is no value - you will not generate the column. I recommend you to check the sample from our online documentation which shows how to implement this command - http://www.telerik.com/help/windows-8-xaml/datagrid-commands-generatecolumncommand.html.
Here's a code snippet which shows how to generate a column:
var context = parameter as GenerateColumnContext;
DataGridTextColumn column = new DataGridTextColumn();
context.Result = column;

I have also logged as a feature request the idea our RadDataGrid to support binding to DynamicObject(ExpandoObject) which will be a more complex solution for such scenarios.

I hope this helps. Let me know if you need further assistance. I have also updated your Telerik points for your valuable feedback.

 

Kind regards,
Ivaylo Gergov
the Telerik team
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
0
Andre
Top achievements
Rank 1
answered on 09 Apr 2013, 08:31 PM
Hi!

I'm trying to achieve the same functionality. I'm trying to use the CustomGenerateColumnCommand class, but where do I check if the property is null? On the CanExecute method? On the Execute method?

Thank you!
0
Ivaylo Gergov
Telerik team
answered on 10 Apr 2013, 04:32 PM
Hi Andre,

You can check if the property is null either in the CanExecute, or in the Execute method, but the better approach would be to check it in the CanExecute method.

I hope this information is useful.


All the best,
Ivaylo Gergov
the Telerik team
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
0
Andre
Top achievements
Rank 1
answered on 11 Apr 2013, 07:11 PM
Hi there! 

I'm sorry. I'm not understanding properly. What should I check?

I have the following code:

public override bool CanExecute(object parameter)
{
    var context = parameter as GenerateColumnContext;
 
    DataGridTextColumn column = new DataGridTextColumn();
    context.Result = column;
 
    return true;
}
 
public override void Execute(object parameter)
{
    var context = parameter as GenerateColumnContext;
}

I tried debugging parameter.Result but it comes null.

Thank you for your help.
0
Ivaylo Gergov
Telerik team
answered on 12 Apr 2013, 11:34 AM
Hello Andre,

For your convenience I have attached a project which shows a sample implementation of such scenario. Basically, if the current property of your ViewModel(context.PropertyName) holds null values - you can return false in the CanExecute method and the column will not be generated.

I hope this helps.

 

Regards,
Ivaylo Gergov
the Telerik team
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
0
Andre
Top achievements
Rank 1
answered on 12 Apr 2013, 01:37 PM
Hi Ivaylo,

I got it working! Thank you very much! 

I did forget one detail: The columns headers are atached to the property names, so the result wasn't very pleasant, as my class as properties just like Yuri showed (which have generic names). I guess I have to find another workaround.

I am also using MVVM. Is there any way to bind the columns to a collection?

Thank you!
0
Ivaylo Gergov
Telerik team
answered on 15 Apr 2013, 07:41 AM
Hi Andre,

In this case you can just set a proper column header for each property in the Execute method. For example:
public override void Execute(object parameter)
{
    var context = parameter as GenerateColumnContext;
    DataGridTextColumn column = new DataGridTextColumn();
    if (context.PropertyName == "Country")
    {
        column.Header = "Pleasant header for Country";
    }           
    context.Result = column;                  
}

As for your other question -- it is not possible to bind the columns to a collection directly. You can either use the auto-generate columns feature (and custom GenerateColumn command to control which columns are generated as described in our previous replies), or you can manually create the columns with code. More information and examples are available in our online documentation.

I hope this helps.


Regards,
Ivaylo Gergov
the Telerik team
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
Tags
Grid for XAML
Asked by
Yuri
Top achievements
Rank 1
Answers by
Ivaylo Gergov
Telerik team
Yuri
Top achievements
Rank 1
Andre
Top achievements
Rank 1
Share this question
or