datagrid doesn't reflect changes in data (unless datatable)

  • Thread starter Thread starter Peter Bladh
  • Start date Start date
P

Peter Bladh

Hi!

After a tip from Alex Feinman I manage to databind a datagrid to an
arraylist (thanks!). But the datagrid doesn't reflect changes in the
arraylist, unless I do the following

dg.DataSource = null;
dg.DataSource = myArrayList;

.... which doesn't seem very effective.
Is there a better way?


Regards
Peter Bladh
 
ArrayList does not expose the IBindingList interface which expose the Event
used to react to changes in the datasource. The only way to refresh the grid
therefore is to manually force it to update using the method described
below.

Peter
 
Thank you for your answer!

Anothor disadvantage with

is that the datagrid won't keep the "scroll location" - if the user has
scrolled down and the datagrid gets updated it will "scroll up". That kind
of behavior will probably upset the users...:)
Is there a way to keep the "scroll location"?


Regards / Peter Bladh
 
Something basically like the following (but I use a DataTable) works for me:

// Save the current item's ID
int oldPsId = this.GetCurrentPackingSlipId(context);
....
dg.DataSource = null;
dg.DataSource = myArrayList;
....
if (oldPsId != 0) {
// Find the index of the old psid in the ArrayList
i = ArrayListIndexOfID(oldPsId); // I used datatable.find here.
if (i >= 0) {
dg.CurrentRowIndex = i;
dg.Select(i);
}
}
 
Back
Top