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
245
Dynamically Sorting when bound to datatable
posted

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:
Alabama
California
Illinois
Iowa
New 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.

Parents
  • 45049
    Suggested Answer
    posted

    whheine said:
    Is there an automatic way to do this that I'm missing?

    Calling RefreshSort() as you've desribed is probably the way to go.

    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.

Reply Children
No Data