Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
50
Maintain XamDataGrid Default Field Layout
posted

I've been struggling for hours with this so hopefully someone can point me in the right direction.

I've got a XamDataGrid with a datasource of a standard .net DataTable.

The XamDataGrid has AutoGenerateFields set to true (since I won't know all the columns until runtime and the number of columns will change over time) and the following *design-time* field layout defined:

<igDP:XamDataGrid.FieldLayouts>
        <igDP:FieldLayout IsDefault="True">
          <igDP:Field Name="First Name">
            <igDP:Field.Settings>
              <igDP:FieldSettings CellClickAction="SelectRecord"
                                  AllowEdit="False" />
            </igDP:Field.Settings>
          </igDP:Field>
            <igDP:Field Name="Last Name">
              <igDP:Field.Settings>
                <igDP:FieldSettings CellClickAction="SelectRecord"
                                    AllowEdit="False" />
              </igDP:Field.Settings>
            </igDP:Field>
            <igDP:Field Name="User"
                        Visibility="Collapsed">
            </igDP:Field>
          </igDP:FieldLayout>
        </igDP:XamDataGrid.FieldLayouts>

The first time I generate my datasource DataTable, everything looks great.  However, when I then add or remove a column in my DataTable, the XamDataGrid creates a new FieldLayout (which I've read is the expected behavior because the AutoGenerateFields property is set to true).  When this new Field Layout is created, however, it does not maintain any of the settings I originally set above in design-time, i.e. my User column is visible (which I don't want) and my two name columns can be edited (which they should not).

I've read on these forums that I can hook into the AssigningFieldLayoutToItem event and use the EventArgs Item property to get the dataitem being assigned to the item and then generate my own FieldLayout but I'm extending the XamDataGrid and this extended class may not always be bound to a DataTable so I don't want to write logic assuming that the Item Property always points to a DataRowView.

Ideally, I'd like to just use the original FieldLayout that works the first time and add/remove fields in the FieldLayoutInitialized event to this original FieldLayout and then use this original FieldLayout in the AssigningFieldLayoutToItem. 

This "appears" to work the first time but on consecutive changes to the DataTable the FieldLayoutInitialized event fails to fire and I'm unable to modify the original FieldLayout as desired.

I've attached two screen shots to show the first time I load data into the grid and what happens when I modify the DataTable and a new FieldLayout is generated.

I also thought about moving the original design-time fields into the new FieldLayout collection but you can't move Fields from one collection to another and I also don't think it's good design to "copy" all the original Field values and settings to the new Fields generated in the new layout but I guess I can go that route if there's no other solutions.

 

 

screenshots.zip
Parents Reply Children
No Data