datagridview控件必须绑定到要排序的ibindinglist对象. [英] datagridview control must be bound to an ibindinglist object to be sorted.

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

问题描述

大家好,

我正在尝试对datagridview进行排序,但是iam出现错误

datagridview控件必须绑定到要排序的ibindinglist对象.

我该怎么解决

我的代码是

hi all,

i am trying to sort datagridview but iam getting below error

datagridview control must be bound to an ibindinglist object to be sorted.

how can i solve this

my code is

private void dg_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                _sortColumn = this.dg.Columns[e.ColumnIndex];
                SortOrder order = this.dg.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection;
                _direction = (order == SortOrder.Descending) ? ListSortDirection.Descending : ListSortDirection.Ascending;
                this.dg.Sort(this.dg.Columns["Images"], _direction);
            }

        }

推荐答案

如果使用自定义对象进行数据绑定,则意味着需要使用SortableBindingList.
这是示例.
If you are using your custom objects for databinding means you need to use the SortableBindingList.
Here is the example.
public Form1()
{
 InitializeComponent();
 
 SortableBindingList<person> persons = new SortableBindingList<person>();
 persons.Add(new Person(1, "timvw", new DateTime(1980, 04, 30)));
 persons.Add(new Person(2, "John Doe", DateTime.Now));
 
 this.dataGridView1.AutoGenerateColumns = false;
 this.ColumnId.DataPropertyName = "Id";
 this.ColumnName.DataPropertyName = "Name";
 this.ColumnBirthday.DataPropertyName = "Birthday";
 this.dataGridView1.DataSource = persons;
}</person></person>


参考


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

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