sort DataGridView和错误DataGridView控件必须绑定到要排序的IBindingList对象 [英] sort DataGridView and error DataGridView control must be bound to an IBindingList object to be sorted

查看:185
本文介绍了sort DataGridView和错误DataGridView控件必须绑定到要排序的IBindingList对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
i想要在用户点击标题栏时对dataGridView进行排序我这样做:

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)
       {
           try
           {


               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;
                   }
                   else
                   {
                       // Sort a new column and remove the old SortGlyph.
                       direction = ListSortDirection.Ascending;
                       oldColumn.HeaderCell.SortGlyphDirection = System.Windows.Forms.SortOrder.None;
                   }
               }
               else
               {
                   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?

推荐答案

根据MSDN InvalidOperationException在

时抛出DataSource属性指定的对象未实现IBindingList接口。

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();



-or-

DataSource属性指定的对象的IBindingList.SupportsSorting属性值为False。


-or-
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;



您可以在StackOverflow上查看可能的解决方案

http://stackoverflow.com/questions/4702014/sorting-datagridview-datasource-on-listt- where-t-is-anonymous [ ^ ]


这篇关于sort DataGridView和错误DataGridView控件必须绑定到要排序的IBindingList对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆