I am trying to get an ultragrid bound to a datatable maintain the sort order when ithe datatable is updated.
I am using UltraGrid v7.3.20073.38.
For example, first, I'll tell the grid that I'm going to maintain the sort myself:
private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e){ e.Layout.Override.HeaderClickAction = HeaderClickAction.ExternalSortSingle}
Then I create a datatable and set up the sort and databinding:
dataTable.Columns.Add("State", Type.GetType("System.String")); dataTable.Rows.Add(new object[] { "Illinois"});dataTable.Rows.Add(new object[] { "Alabama" });dataTable.Rows.Add(new object[] { "New York" });dataTable.Rows.Add(new object[] { "California" });
dataTable.DefaultView.Sort = "State ASC";
this.dataGridView1.DataSource = dataTable.DefaultView;
However if I now try and Add a row:
dataTable.Rows.Add(new object[] { "Iowa" });
the grid doesn't resort. Instead it looks like:
"Alabama" "California""Illinois""New York""Iowa"
The frustrating thing here is that if I examine the DataView on the DataTable, it IS sorted like this:AlabamaCaliforniaIllinoisIowaNew York
Also, if I perform the same test with the .NET DataGridView control, it does perform the sort for me. The only way I have been able to get the Ultragrid to do this is by calling:
ultraGrid1.DisplayLayout.Bands[0].SortedColumns.RefreshSort(true);
after the row is added or the data is updated. Is there an automatic way to do this that I'm missing?
Thanks.
I have found the answer by reading (actually re-reading) Mike Saltzman's performance guide. This works:
ultraGrid1.BeginUpdate();ultraGrid1.SuspendRowSynchronization();
dataTable.BeginLoadData();dataTable.LoadDataRow(new object[] { "Iowa" }, LoadOption.OverwriteChanges);dataTable.EndLoadData();
ultraGrid1.ResumeRowSynchronization();ultraGrid1.EndUpdate();
whheine said:Is there an automatic way to do this that I'm missing?
Some programmers wouldn't want the sort to be immediately applied as rows are added, since this might cause the user interface to "jump" and cause the user to lose track of where they're at. It's a much simpler case to not reapply the sort by default, and to use one line of code if the functionality is desired, than for us to reapply the sort automatically and to have people disable it if they don't want it.