通过编程排序绑定到DataGridView的Sortable BindingList [英] Sortable BindingList bound to DataGridView with programmatic sort

查看:290
本文介绍了通过编程排序绑定到DataGridView的Sortable BindingList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了在 http://msdn.microsoft.com/zh-CN上找到的SortableSearchableList类/library/aa480736.aspx ,并向其中添加了一种Sort方法,如下所示:

I have implemented the SortableSearchableList class found at http://msdn.microsoft.com/en-us/library/aa480736.aspx and have added a Sort method to it as follows:

public void Sort(PropertyDescriptor prop, ListSortDirection direction)
{
ApplySortCore(prop, direction);
}

此类通过单击任何列标题对DataGridView进行排序时起作用,但是我需要能够以编程方式调用指定列的Sort方法(在此示例中使用sortButton控件)。我在网上找到的一些代码示例建议为该列获取PropertyDescriptor并将其传递给ApplySortCore方法。我还没有开始工作。我可以获取DataGridView或SortableSearchableList的PropertyDescriptorCollection属性,但似乎无法获取Find方法来获取指定列/成员的PropertyDescriptor。这是我其余的代码:

This class works when sorting my DataGridView by clicking on any of the column headers, but I need to be able to programmatically call the Sort method for a specified column (using a sortButton control in this example). The few code examples I've found online suggest obtaining the PropertyDescriptor for the column and passing it along to the ApplySortCore method. I have yet to get that to work. I can get the PropertyDescriptorCollection properties of either my DataGridView or SortableSearchableList, but can't seem to get the Find method to obtain the PropertyDescriptor for my specified column/member. Here's the rest of my code:

namespace SortableBindingListTest
{
public partial class Form1 : Form
{
    private SortableSearchableList<Tags> alarms = new SortableSearchableList<Tags>();
    public Form1()
    {
        InitializeComponent();
        alarms.Add(new Tags("some text", "1"));
        alarms.Add(new Tags("more text", "2"));
        alarms.Add(new Tags("another one", "3"));
        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.AllowUserToAddRows = true;
        dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
        dataGridView1.RowHeadersVisible = false;
        dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
        DataGridViewTextBoxColumn alarmColumn = new DataGridViewTextBoxColumn();
        alarmColumn.DataPropertyName = "Alarm";
        alarmColumn.Name = "Alarm";
        alarmColumn.HeaderText = "Alarm";
        DataGridViewTextBoxColumn messageColumn = new DataGridViewTextBoxColumn();
        messageColumn.DataPropertyName = "Message";
        messageColumn.Name = "Message";
        messageColumn.HeaderText = "Message";
        dataGridView1.Columns.Add(alarmColumn);
        dataGridView1.Columns.Add(messageColumn);
        dataGridView1.DataSource = alarms;
    }

    private void sortButton_Click(object sender, EventArgs e)
    {
        // try getting properties of BindingList
        PropertyDescriptorCollection listProperties = TypeDescriptor.GetProperties(alarms);
        PropertyDescriptor alarmProp = listProperties.Find("Alarm", false);
        // prop is null at this point, so the next line fails
        alarms.Sort(alarmProp, ListSortDirection.Ascending);
        // try getting properties of DataGridView column
        PropertyDescriptorCollection dgvProperties = TypeDescriptor.GetProperties(dataGridView1);
        PropertyDescriptor columnProp = dgvProperties.Find("Alarm", false);
        // columnProp is null at this point, so the next line also fails
        alarms.Sort(columnProp, ListSortDirection.Ascending);
    }
}

public class Tags : INotifyPropertyChanged
{
    private string _alarm;
    private string _message;
    public event PropertyChangedEventHandler PropertyChanged;
    public Tags(string alarm, string message)
    {
        _alarm = alarm;
        _message = message;
    }

    public string Alarm
    {
        get { return _alarm; }
        set
        {
            _alarm = value;
            this.NotifyPropertyChanged("Alarm");
        }
    }
    public string Message
    {
        get { return _message; }
        set
        {
            _message = value;
            this.NotifyPropertyChanged("Message");
        }
    }
    private void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}
}

任何帮助将不胜感激。

推荐答案

尝试一下

dataGridView1.Sort(dataGridView.Columns[0],ListSortDirection.Ascending);

这篇关于通过编程排序绑定到DataGridView的Sortable BindingList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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