Forums

Error when trying to create a data grid with a variable number of columns
Last Post 10 Sep 2016 01:05 PM by Filip Dušek. 8 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
A TUser is Offline
New Member
New Member
Posts:34


--
19 Jul 2016 09:53 PM
    I want to make a data grid whose columns can be set at runtime. I've found this solution (other solutions I've seen require setting columns programmatically, not in XAML).

    When I'm attempting to use the solution mentioned above, UI generator successfully generates code, but this code doesn't compile due to the following error:
    'DataGridColumnHeader' does not contain a definition for 'ContentTemplateProperty'

    The code which causes an error:
    Setter e_3_Col0_h_s_S_3 = new Setter(DataGridColumnHeader.ContentTemplateProperty, new DataTemplate(e_3_Col0_h_s_S_3_dtFunc));
    

    Also, if there are other ways to implement a data grid with a variable number of columns, I would be glad to know about them.
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:674


    --
    19 Jul 2016 10:17 PM
    This kind of stuff is tricky even for WPF and using Silverlight tricks won't help much, because I implemented my UI based on WPF features. This sounds more like completely new control, DataGrid wasn't designed for it.

    Do you have limited number of columns?
    A TUser is Offline
    New Member
    New Member
    Posts:34


    --
    20 Jul 2016 11:27 PM

    This kind of stuff is tricky even for WPF and using Silverlight tricks won't help much, because I implemented my UI based on WPF features. This sounds more like completely new control, DataGrid wasn't designed for it.


    Oh, I didn't know it works only for Silverlight. Strange that the project was compiled and UI generator ran without any problems. Maybe UI generator should return error in such cases?


    Do you have limited number of columns?


    Yes, I probably need no more than 10 columns if that's what you meant. I guess you'll suggest to create a fixed number of columns and bind their headers to values in a list populated at runtime. I tried to do this but didn't know how to remove or hide unused columns. But right now I've checked and found Visibility property DataGridTextColumn so this solution should work for me, will try it.

    But if you wanted to suggest something other, please tell, maybe it will be even better.
    A TUser is Offline
    New Member
    New Member
    Posts:34


    --
    23 Jul 2016 01:44 AM

    But right now I've checked and found Visibility property DataGridTextColumn



    The trouble is that binding for this property doesn't work. No matter what value I'm trying to bind, a column is visible. Probably this is a bug.
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:674


    --
    25 Jul 2016 09:46 AM
    I will check that out.
    A TUser is Offline
    New Member
    New Member
    Posts:34


    --
    28 Aug 2016 09:03 PM
    Any update on this? Did you reproduce this bug? Is it fixed?
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:674


    --
    29 Aug 2016 10:42 AM
    I checked Visibility on columns and fixed few bugs, which sometime makes UI hang in never ending loop. I think problem will be binding to this property, because there is not DataContext. You could just get the control directly and set columns visibility in code.
    A TUser is Offline
    New Member
    New Member
    Posts:34


    --
    08 Sep 2016 08:59 PM
    Setting columns visibility in code is very inconvenient (if I understand right what did you mean) because all members of the generated class are private.

    Could you elaborate what does it mean there is no DataContext? What prevents me from using the following code?

    <DataGridTextColumn Binding="{Binding Values[0]}" Visibility="{Binding Columns[0].IsVisible}" >
        <DataGridTextColumn.Header>
            <TextBlock Text="{Binding Columns[0].Header}" />
        </DataGridTextColumn.Header>
    </DataGridTextColumn>
    


    Where Columns is an array of ColumnViewModels, a class I already use for setting headers.
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:674


    --
    10 Sep 2016 01:05 PM
    yes, inconvenient, tho you can make partial class for generated class and make any member public.

    The issue is that Column itself is not part of visual tree so it does not have any Data Context. It's bit tricky to add them to visual tree. I need to do some changes there, so hopefully it will be possible in future.
    You are not authorized to post a reply.