I have a grid that has 10 Columns defined in it. In code-behind I dynamically bind the ItemsSource to different collections.
One collection has all 10 columns. Another collection only has 5 of them.
When the code switches to the collection with only 5 columns, I get this error
The following key(s) do not correspond with the DataSource etc, etc.
Because that column doesn't exist in the other collection.
I tried a hack. I tried adding 1 column to the grid, and in codebehind, dynamically setting the Key to this one dummy property while setting visibility to collapsed. It binds, but you get the weirdest glitch. The grid does some visual stuff I can't even explain.
Can anyone think of a better way to accomplish this? I considered adding fake properties that I can bind to but adding 5 fake properties to the underlying object is less than ideal if there is a better solution. I could also have 2 grids, hiding or showing whichever but again, it gets messy. I could build the columns up dynamically also, but again, very very messy.
Any ideas would be appreciated.
I would look at using the grids type matching feature to solve this problem. the feature allows you to tell the grid to select a column layout to use based on the object type in the bound items source. So for example, say I have two classes, ClassA and ClassB, each having different properties. I can create two different ColumnLayouts and add them to the XamGrids ColumnLayouts collection (see the snippet below) and provide them with the type names. At runtime the grid will select the ColumnLayout to use by matching the type of the items source to the type specified on the ColumnLayout.
<ig:XamGrid x:Name="xamGrid1"> <ig:XamGrid.ColumnLayouts>
<ig:ColumnLayout Key="Layout1" TargetTypeName="ClassA"> <ig:ColumnLayout.Columns> <ig:TextColumn Key="PropA" /> <ig:TextColumn Key="PropB" /> <ig:TextColumn Key="PropC" /> </ig:ColumnLayout.Columns> </ig:ColumnLayout>
<ig:ColumnLayout Key="Layout2" TargetTypeName="ClassB"> <ig:ColumnLayout.Columns> <ig:TextColumn Key="PropA" /> <ig:TextColumn Key="PropB" /> <ig:TextColumn Key="PropC" /> <ig:TextColumn Key="PropD" /> <ig:TextColumn Key="PropE" /> <ig:TextColumn Key="PropF" /> </ig:ColumnLayout.Columns> </ig:ColumnLayout>
</ig:XamGrid.ColumnLayouts></ig:XamGrid>
Note that the ColumnLayout still needs a Key, but in this scenario it just serves as a unique ID for the layout, its not tied to any propery of the ItemsSource.
Hope that helps.
Devin
Devin,
That worked perfectly.
Thanks!
I have a couple minor problems with that solution that have cropped up after more testing and am hoping maybe you can help me.
Both these problems are very obscure. The first column in the first column layout is an ImageColumn. This image column binds to one of the properties on the TargetTypeName object. That property returns a BitmapImage. As soon as I added the columnlayout functionality you mentioned I noticed that only when the ImageColumn had an actual image in it, then I tried to reset the grid.ItemsSource to the second layout it would completely crash the application. No error, no exception. It would just kill the application. I was able to fix this issue. The user control with the grid is a singleton and has a cleanup method. I just set grid.ItemsSource to null and this fixed that issue.
I just wanted to point out that issue, even though I worked around it. I couldn't set the grid.ItemsSource to null and then the next line set it to the collection as it would still cause the problem. At a different spot in our infrastructure (as I unload a view from a PRISM region and inject a new user control/view, I call a cleanup method to free up memory). It seemed to work doing it there.
Second problem and this one is even harder to explain. Since I added the multiple column layout I've noticed that after the ItemsSource is set multiple times (usually happens after 3 times) that as soon as I set the ItemsSource to a collection that is of the second columnlayout's TargetTypeName I get this weird mystery character showing up in the upper left hand corner of the first column for the life of the control. The character appears to be a character from the filter row. I will include a screen shot.
Here is my xaml (removed a couple columns to make it smaller)
<ig:XamGrid.FilteringSettings> <ig:FilteringSettings AllowFiltering="FilterRowTop" /></ig:XamGrid.FilteringSettings><ig:XamGrid.SortingSettings> <ig:SortingSettings AllowSorting="True" AllowMultipleColumnSorting="True" ShowSortIndicator="True" /></ig:XamGrid.SortingSettings><ig:XamGrid.ColumnLayouts> <ig:ColumnLayout Key="Transactions" TargetTypeName="droTxns"> <!-- Transactions Grid Column Layout--> <ig:ColumnLayout.Columns> <ig:ImageColumn Key="Status" HeaderText=" " IsResizable="False" /> <ig:TextColumn Key="CompanyName" HeaderText="Company" /> <ig:TextColumn Key="TxnAmount" HeaderText="Amount" FormatString="{}{0: ### ### ###.00}" /> <ig:TextColumn Key="DateChanged" HeaderText="Date Changed" /> <ig:TextColumn Key="TxnID" HeaderText="" Visibility="Collapsed" /> </ig:ColumnLayout.Columns> </ig:ColumnLayout> <ig:ColumnLayout Key="Templates" TargetTypeName="droTemplates"> <!-- Templates Grid Column Layout--> <ig:ColumnLayout.Columns> <ig:TextColumn Key="TxnType" HeaderText="Document Type" /> <ig:TextColumn Key="StandardVersion" HeaderText="Version" /> <ig:TextColumn Key="DateChanged" HeaderText="Date Changed" /> <ig:TextColumn Key="TxnID" HeaderText="" Visibility="Collapsed" /> </ig:ColumnLayout.Columns></ig:ColumnLayout></ig:XamGrid.ColumnLayouts>
If you or anyone has any idea what could be causing the mystery letter showing please let me know.
HI,
Can you send a small isolated sample that reproduces this issue.
Sincerely,
Matt Developer Support Engineer
Matt,
Originally I thought the issue was created by my manipulation of the ItemsSource property but I later discovered it has to do with setting the AllowFiltering property dynamically in code-behind. I created a small sample in this post
http://community.infragistics.com/forums/p/45367/246041.aspx#246041
I believe it should be fixed in the next service release.
Thanks,
Ken