I have a XamGrid and a combobox on my xaml page. Based on what value I select in the combobox, WCF returns the data and I set it as the ItemSource of the XamGrid. I am using DataTable to bind the data to the grid.
public partial class Report: Page { private DataTable resultData; . . void proxy_GetDataCompleted(object sender, EventArgs e) { resultData = new DataTable(e.Result); webGridView.ItemSource = resultData; }
Now everytime I change the selection on the combobox, I do get different datasource from the WCF service (dataset has different columns) and even the data is bound correctly on the UI. For the first time the data is bound, I do see a valid count in webGridView.Columns, however for subsequent databindings, the data seems nicely bound on the grid/UI but the count on webGridView.Columns is 0 and I cannot access each of the columns. Even if I call invalidateData() before binding the new data source, it doesnt work. I apply formatting to the data programaticallly. Now since I cant access the columns when the itemsource has changed, the formatting rules arent applied. How do I fix it?
Hi,
So the xamGrid doesn't immediately walk through the data when the ItemsSource changes. It only does so once the data is accessed.
This will generally happen in the next Measure phase, however you can get it sooner, by accessing a row.
Row r = this.grid1.Rows[0];
or, you can wait by using BeginInvoke:
this.Dispatcher.BeginInvoke(() =>
{
int count = this.grid.Columns.Count;
});
Hope this helps,
-SteveZ
SteveZ, this solution doesn't seem to work. I tried both the methods you suggested. I observed that the binding fails for every alternate attempt to bind the data (first itemsource-> works, second itemsource -> fails, third itemsource-> works, fourth -> fails and so on)
Not sure if this makes any difference but there is the method I try to call to format the data after setting the itesmsource.
private void ApplyFormatting(DataTable dtFormat) { foreach (DataColumn col in resultData.Columns) { if(col.DataType == typeof(Double) || col.DataType == typeof(Decimal)) { TextColumn c = (TextColumn)webGridView.Columns[col.ColumnName]; if (c != null) { c.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right; LessThanConditionalFormatRule less = new LessThanConditionalFormatRule(); less.Value = 0; less.StyleToApply = Resources["NegativeValue"] as Style; c.ConditionalFormatCollection.Add(less); } } else if(col.DataType == typeof(DateTime)) { TextColumn c = (TextColumn)webGridView.Columns[col.ColumnName]; if (c != null) c.FormatString = "{0:d}"; } }
Thanks Harshb, setting itemsource as null before rebinding works.
Also, I am trying to set SumSummaryOperand's IsApplied property as true for all numeric columns in the code behind but it doesnt apply. Is this related issue?
foreach (SummaryOperandBase operand in col.SummaryColumnSettings.SummaryOperands) { if (operand is SumSummaryOperand) operand.IsApplied = true; }
SteveZ, any updates on this?
I tried your sample, and yes you'll need to set the ItemSource to null to make sure and clear out any cached information.
However, the Summary code you posted appears to work just fine.
One note, it won't get into the code in your sample in the initial call to that method, b/c the xamGrid hasn't loaded, and the ItemsSource will not load until the xamGrid has been loaded, so you may need to delay the call the initial time.
Yes, summary doesnt seem to be applied even if call it after this.Dispatcher.BeginInvoke. How do I get this to work with the code? The datasources we use in our application is dynamic as shown in the sample and therefore I would need a workaround. Any help will be appreciated. Thanks
I would recommend that you first have some logic that looks to see if the xamGrid is loaded by using the xamGrid Loaded event. If the comboBox changed before the xamGrid is loaded you can then call a method that performs your column logic.
As for the summaries, as long as you're setting the ItemsSource to null, that will invalidate the columns that were previously there, and allow you apply the summaries.