Click here to Skip to main content
15,170,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i want to sort dataGridView when user click on header column i do it :
dgvList.DataSource = (_db.UserProfiles
                   .OrderBy(r => r.Family)).ToList();

private void dgvList_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
           foreach (DataGridViewColumn column in dgvList.Columns)
               column.SortMode = DataGridViewColumnSortMode.Programmatic;


       private void dgvList_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)

               DataGridViewColumn newColumn = dgvList.Columns[e.ColumnIndex];
               DataGridViewColumn oldColumn = dgvList.SortedColumn;
               ListSortDirection direction;

               // If oldColumn is null, then the DataGridView is not sorted.
               if (oldColumn != null)
                   // Sort the same column again, reversing the SortOrder.
                   if (oldColumn == newColumn &&
                       dgvList.SortOrder == System.Windows.Forms.SortOrder.Ascending)
                       direction = ListSortDirection.Descending;
                       // Sort a new column and remove the old SortGlyph.
                       direction = ListSortDirection.Ascending;
                       oldColumn.HeaderCell.SortGlyphDirection = System.Windows.Forms.SortOrder.None;
                   direction = ListSortDirection.Ascending;

               // Sort the selected column.
               dgvList.Sort(newColumn, direction);
               newColumn.HeaderCell.SortGlyphDirection =
                   direction == ListSortDirection.Ascending ?
                  System.Windows.Forms.SortOrder.Ascending : System.Windows.Forms.SortOrder.Descending;

           catch (Exception ex)

               MessageBox.Show(ex.Message, "خطا!", MessageBoxButtons.OK, MessageBoxIcon.Error);

when i click on header columns give me error:
DataGridView control must be bound to an IBindingList object to be sorted

now what can i do?

1 solution

According to MSDN InvalidOperationException is thrown when
The object specified by the DataSource property does not implement the IBindingList interface.
// example 1 (from the question)
dgvList.DataSource = _db.UserProfiles.OrderBy(r => r.Family).ToList();

The object specified by the DataSource property has a IBindingList.SupportsSorting property value of False.
// example 2
var list =_db.UserProfiles.OrderBy(r => r.Family).ToList();
var bs = new BindingSource();
bs.DataSource = list;
dgvList.DataSource = bs;

you can look at a possible solution on StackOverflow[^]
NorouziFar 9-Jun-14 12:47pm
i do it example 2 , but not work and give me error again
Alexander Sharykin 10-Jun-14 2:16am
it will, because example 2 is an Exception situation
try to use any of StackOverflow solution
I would suggest to create DataTable and fill it with data from _db.UserProfiles as the easies variant
smth like this:
var list = _db.UserProfiles.OrderBy(r => r.Family).ToList();
var dt = new DataTable();
foreach(var user in list)
dgvList.DataSource = dt;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900