Question

Im working on a winforms application using devexpress GridControl and i have a ViewModel with two porperties

public class MyViewModel
{
    public List<Cusomer> Customers{get;set;}
    public Customer SelectedCustomer {get;set;}

}

How can i bind the GridControl's SelectedRow to my ViewModel SelectedCustomer porperty ?

thanks

Was it helpful?

Solution

Not sure if there is a way to bind the property directly. You can instead capture the event triggered in BindingSource when selection is changed.

1) Add a event handler for the CurrentChanged event:

public class MyViewModel
{
    public List<Customer> Customers { get; set; }
    public Customer SelectedCustomer { get; set; }

    public void BindingSourceCurrentChanged(object sender, EventArgs e)
    {
        var bindingSource = sender as BindingSource;
        if (bindingSource == null) return;

        SelectedCustomer = bindingSource.Current as Customer;
    }
}

2) Link the GridControl, BindingSource and ViewModel together:

BindingSource bindingSource = new BindingSource { DataSource = myViewModel.Customers };
bindingSource.CurrentChanged += myViewModel.BindingSourceCurrentChanged;
gridControl1.DataSource = bindingSource;
gridView1.PopulateColumns();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top