s.o.s
s.o.s

Reputation: 1

How can I handle this prolbem "DataGridView control must be bound to an IBindingList object to be sorted."?

Here is my code:

 using (var context = new NorthwindContext())
            {
                var info = (from p in context.Products
                            join od in context.OrderDetails on p.ProductId equals od.ProductId
                            join c in context.Categories on p.CategoryId equals c.CategoryId
                            join o in context.Orders on od.OrderId equals o.OrderId
                            join e in context.Employees on o.EmployeeId equals e.EmployeeId
                            join cu in context.Customers on o.CustomerId equals cu.CustomerId
                            join s in context.Suppliers on p.SupplierId equals s.SupplierId
                            select new
                            {
                                o.OrderId,
                                cu.ContactName,
                                s.CompanyName,
                                e.FirstName,
                                p.ProductName,
                                c.CategoryName,
                                o.OrderDate,
                                o.Freight,
                            }).ToList();
                dgv.DataSource = info;
                dgv.Refresh();
            }

But when I want to sort data by [Freight]:

dgv.Sort(dgv.Columns["Freight"], ListSortDirection.Ascending);

Then I got a problem mentioned in title above. I need your help!

I got a problem mentioned in title above. I need your help!

Upvotes: 0

Views: 57

Answers (1)

Karen Payne
Karen Payne

Reputation: 5157

To provide sorting in the DataGridView, first change the current statement from an anonymous type to a strong type.

Note In the code which follows my NorthWind database has different property names so you need to adjust them e.g. CustomerIdentifier back to CustomerId

Container

public class DataContainer
{
    public int OrderId { get; }
    public string CompanyName { get; }
    public string FirstName { get; }
    public string ProductName { get; }
    public string CategoryName { get; }
    public DateTime? OrderDate { get; }
    public decimal? Freight { get; }

    public DataContainer(
        int orderId, 
        string companyName, 
        string firstName, 
        string productName, 
        string categoryName, 
        DateTime? orderDate, 
        decimal? freight)
    {
        OrderId = orderId;
        CompanyName = companyName;
        FirstName = firstName;
        ProductName = productName;
        CategoryName = categoryName;
        OrderDate = orderDate;
        Freight = freight;
    }
}

Code to read data

public class StackoverflowOperations
{
    public static List<DataContainer> ReadData()
    {
        using (var context = new NorthwindContext())
        {
            return (from p in context.Products
                join od in context.OrderDetails on p.ProductId equals od.ProductId
                join c in context.Categories on p.CategoryId equals c.CategoryId
                join o in context.Orders on od.OrderId equals o.OrderId
                join e in context.Employees on o.EmployeeId equals e.EmployeeId
                join cu in context.Customers on o.CustomerIdentifier equals cu.CustomerIdentifier
                join s in context.Suppliers on p.SupplierId equals s.SupplierId
                select new DataContainer(o.OrderId, cu.CompanyName, e.FirstName, p.ProductName, c.CategoryName,
                    o.OrderDate, o.Freight))
                .ToList();
        }
    }
}

Add the class in the following post to your project.

Form code, one button one DataGridView showing an example for sorting and sorting works by clicking column headers also.

public partial class Form1 : Form
{
    private SortableBindingList<DataContainer> _dataContainers;
    private BindingSource _source = new BindingSource();
    public Form1()
    {
        InitializeComponent();
        Shown += OnShown;
    }

    private void OnShown(object? sender, EventArgs e)
    {
        _dataContainers = new SortableBindingList<DataContainer>(StackoverflowOperations.ReadData());
        _source.DataSource = _dataContainers;
        dataGridView1.DataSource = _source;
    }
    private void SortButton_Click(object sender, EventArgs e)
    {
        _source.Sort = "CategoryName DESC";
    }
}

Upvotes: 1

Related Questions