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 SilverDev,
Before binding datasource again you set Datasource to null and after that you rebind grid with new data .Below is the code snippet for this.
webGridView.ItemSource=null;
webGridView.ItemSource = resultData;
Alright, here you go. I could reproduce this bug and attached is the solution. Please provide me a workaround ASAP as this is critical.
Hi,
Can you attach a sample that demonstrates your problem then?
-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}"; } }
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,