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
225
Ultragrid and Databinding, datasource lagging behind the grid
posted

I've found several posts here regarding the OPPOSITE of this (forcing the grid to reflect new changes to the datasource), but the problem I'm having is exactly the other way round.

I've bound a simple BindingList<> to the grid, it displays exactly as I would expect.

One of the columns is a bool value, so it's displayed as a checkbox.

What I need is for the datasource to be updated when I change the state of any of those checkboxes.

I can intercept the ValueChanged, or the CellChanged events, but when I evaluate the DataSource from those events, it hasn't yet been updated via databinding. it's always lagging behind.

Here's a small sample
Just create a form with an ultragrid called GRID

and paste this in.

private void Form_Load(object sender, EventArgs e)
{
    var l = new BindingList<Entry>();
    l.Add(new Entry() { Name = "Name1", Selected = true });
    l.Add(new Entry() { Name = "Name2", Selected = false});
    l.Add(new Entry() { Name = "Name3", Selected = false });
    l.Add(new Entry() { Name = "Name4", Selected = true });
 
 
    grid.SetDataBinding(l, "");
 
    //var c = grid.DisplayLayout.Bands[0].Columns[nameof(Entry.Selected)];
    //c.Editor.ValueChanged += (s, a) => { WriteEntries(); };
 
    //grid.CellChange += (s, a) => { WriteEntries(); } ;
    grid.AfterCellUpdate += (s, a) => { WriteEntries(); };
}
 
private void WriteEntries()
{
    var l = (BindingList<Entry>)grid.DataSource;
    var b = string.Join(",", l.Where(i => i.Selected).Select(i => i.Name));
 
    System.Diagnostics.Debug.WriteLine($"Selected: " + b);
}
 
public class Entry
{
    public string Name { get; set; }
    public bool Selected { get; set; }
}

You can see the "WriteEntries" function simply evaluates the items in the BindingList and outputs the list of those rows that are checked.

When I run this, the output list is always missing the state of the last element that was toggled.

I'm guessing this is because within the given event, the databinding manager hasn't yet bee triggered to push the change to the grid back into the datasource.

But I'm at a loss as to how to manually perform that update.

I know there are other ways to accomplish this (for instance, avoid databinding and just query the state of the column of checks directly), but I'm looking for a solution using databinding if at all possible.

Parents Reply Children
No Data