Ahmad.H
Ahmad.H

Reputation: 1

How Can i Convert datagridview datasource to BindingListView (Equin.ApplicationFramework.BindingListView)

I set this DataBindingComplete event to my datagridview. I want every datasource that binding to datagridview can be sortable by clicking on column.

void MakeColumnsSortable_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {

        DataGridView dataGridView = sender as DataGridView;
        foreach (DataGridViewColumn column in dataGridView.Columns)
                column.SortMode = DataGridViewColumnSortMode.Automatic;

    }

all of my datasource is List and bindingsource doesnot sort when my list is ended by .ToList Now . how can i convert datagridview.datasource to Equin.ApplicationFramework.BindingListView and set it again to datasource for make any datagridview sortable?

Upvotes: 0

Views: 1551

Answers (1)

Harald Coppoolse
Harald Coppoolse

Reputation: 30454

Proper usage of Equin.ApplicationFramework.BindingListView would be as follows

Upon creation of your form:

  • Create a BindingListView. It will be filled later with the items that you want to display / sort / filter
  • Create a BindingSource.
  • Create a DataGridView.
  • Add the columns to the properties you want to show

The latter three steps can be done in visual studio designer. If you do that, The code will be in InitializeComponents.

Suppose you want to show / sort / filter elements of MyType Your form will be like:

public class MyForm : Form
{
    private BindingListView<MyType> MyItems {get; set;}

    public MyForm()
    {
        InitializeComponent();

        this.MyItems = new BindingListView<MyType>(this.components);
        // components is created in InitializeComponents
        this.MyBindingSource.DataSource = this.MyItems;
        this.MyDataGridView.DataSource = this.MyBindingSource;

        // assigning the DataPropertyNames of the columns can be done in the designer,
        // however doing it the following way helps you to detect errors at compile time
        // instead of at run time
        this.columnPropertyA = nameof(MyType.PropertyA);
        this.columnPropertyB = nameof(MyType.PropertyB);
        ...
    }

You can do without the BindingSource, you can assign the BindingListViewdirectly to the DataSource of the DataGridView. Sorting and Filtering will still work. However the BindingSource will help you to access the currently selected item.

private MyType SelectedItem
{
    get {return ((ObjectView<MyType>)this.MyBindingSource.Current)?.Object; }
}

private void DisplayItems (IEnumerable<MyType> itemsToDisplay)
{
    this.MyItems.DataSource = itemsToDisplay.ToList();
    this.MyItems.Refresh(); // this will update the DataGridView
}

private IEnumerable<MyType> DisplayedItems
{
    get {return this.MyItems; }
    // BindingListview<T> implements IEnumerable<T>
}

This is all. You don't need to create special functions to sort on mouse clicks. The sorting will be done automatically inclusive deciding on the sort order and displaying the correct sorting glyphs. If you want to sort programmatically:

// sort columnPropertyA in descending order:
this.MyDataGridView.Sort(this.columnPropertyA.ListsortDirection.Descending);

One of the nice things about the BindingListView is the filtering option:

// show only items where PropertyA not null:
this.MyItems.ApplyFilter(myItem => myItem.PropertyA != null);

// remove the filter:
this.MyItems.RemoveFilter();

(I'm not sure if a Refresh() is needed after applying or removing a filter

Upvotes: 2

Related Questions