【问题标题】:Sortable BindingList bound to DataGridView with programmatic sort通过编程排序绑定到 DataGridView 的可排序 BindingList
【发布时间】:2012-04-27 20:32:11
【问题描述】:

我已经实现了位于http://msdn.microsoft.com/en-us/library/aa480736.aspx 的 SortableSearchableList 类,并为其添加了一个 Sort 方法,如下所示:

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

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

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

任何帮助将不胜感激。

【问题讨论】:

    标签: c# winforms data-binding datagridview bindinglist


    【解决方案1】:

    试试这个

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

    【讨论】:

      猜你喜欢
      • 2017-09-20
      • 2018-01-10
      • 1970-01-01
      • 2010-09-19
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多