【问题标题】:Sort a column by clicking on a Button not on the ColumnHeader of a DataGrid c#通过单击不在 DataGrid c# 的 ColumnHeader 上的按钮对列进行排序
【发布时间】:2017-08-25 19:53:26
【问题描述】:

我有一个带有HeadersVisibility="None" 的DataGrid。 我想在 DataGrid 之外创建一个 Button,其作用与单击 DataGrid 的 n 列完全相同(对列排序)。

我尝试了以下代码(myDataGrid 是我的 DataGrid 的名称):

private void button_Click(object sender, RoutedEventArgs e)
{
     DataGridColumn column = myDataGrid.Columns[0];
     DataGridColumnHeader columnHeader = GetColumnHeaderFromColumn(column);
     columnHeader.OnClick();
}

但函数 DataGridColumnHeader.OnClick() 由于其保护级别而无法访问。

我还查看了namespace System.Windows.Controls 中的class DataGrid,在那里我找到了这个:

public event DataGridSortingEventHandler Sorting;

/// <summary>
/// Protected method which raises the sorting event and does default sort
/// </summary>
/// <param name="eventArgs"></param>
protected virtual void OnSorting(DataGridSortingEventArgs eventArgs)
{
    eventArgs.Handled = false;
    if (Sorting != null)
    {
        Sorting(this, eventArgs);
    }

    if (!eventArgs.Handled)
    {
        DefaultSort(
                eventArgs.Column,
                /* clearExistinSortDescriptions */
                (Keyboard.Modifiers & ModifierKeys.Shift) != ModifierKeys.Shift);
    }
}

/// <summary>
/// Method to perform sorting on datagrid
/// </summary>
/// <param name="sortColumn"></param>
internal void PerformSort(DataGridColumn sortColumn)
{
    Debug.Assert(sortColumn != null, "column should not be null");

    if (!CanUserSortColumns || !sortColumn.CanUserSort)
    {
        return;
    }            
    if (CommitAnyEdit())
    {
        PrepareForSort(sortColumn);

        DataGridSortingEventArgs eventArgs = new DataGridSortingEventArgs(sortColumn);
        OnSorting(eventArgs);

        if (Items.NeedsRefresh)
        {
            try
            {
                Items.Refresh();
            }
            catch (InvalidOperationException invalidOperationException)
            {
                Items.SortDescriptions.Clear();
                throw new InvalidOperationException(SR.Get(SRID.DataGrid_ProbableInvalidSortDescription), invalidOperationException);
            }
        }
    }
}

但我无法使用它。 关于我该怎么做的任何想法?

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    您应该改用SortDescriptions 集合。此集合允许您提供一组规则来对视图中的项目进行排序。

    你可以在这里找到一个很好的例子来说明如何使用它:https://stackoverflow.com/a/19952233/6597895

    public static void SortDataGrid(DataGrid dataGrid, int columnIndex = 0, ListSortDirection sortDirection = ListSortDirection.Ascending)
    {
        var column = dataGrid.Columns[columnIndex];
    
        // Clear current sort descriptions
        dataGrid.Items.SortDescriptions.Clear();
    
        // Add the new sort description
        dataGrid.Items.SortDescriptions.Add(new SortDescription(column.SortMemberPath, sortDirection));
    
        // Apply sort
        foreach (var col in dataGrid.Columns)
        {
            col.SortDirection = null;
        }
        column.SortDirection = sortDirection;
    
        // Refresh items to display sort
        dataGrid.Items.Refresh();
    }
    

    【讨论】:

      【解决方案2】:
      1. 您必须对“dataView”应用排序并将其重新绑定到 dataGrid。

        private void SortButton_Click(object sender, System.EventArgs e)
        {
           if(order == SORT_ORDER.ASC)
           {
             //Ascending   
             this.BindData(SORT_ORDER.DESC);
           }
           else
           {
             //Descending   
             this.BindData(SORT_ORDER.ASC);
           }
        }
        
        private void BindData(SORT_ORDER order)
        {
           //here read data from your datasource, by implementing that logic in 'GetDataSource()' function
            DataView dv = this.GetDataSource().DefaultView;
        
            if(order == SORT_ORDER.ASC)
            {
                dv.Sort = "Address ASC";
                this.SortLabel.Text = "Current sorting order is ascending";
            }
            else
            {
                dv.Sort = "Address DESC";
                this.SortLabel.Text = "Current sorting order is descending";
            }
            this.dgTest.DataSource = dv;
        }
        

      或者

      1. 如果您可以使用 DataGridView,那就容易多了:

      this.myDataGrid.Sort(this.dataGridViewTextBoxColumn1, ListSortDirection.Descending);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-24
        • 2014-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-12
        • 2014-12-18
        相关资源
        最近更新 更多