通过单击DataGrid的ColumnHeader上没有的按钮对列进行排序c#

时间:2017-03-31 13:51:22

标签: c# wpf datagrid

我有一个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()无法访问。

我还看了class DataGrid namespace System.Windows.Controls我找到的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); } } } }

public class CarDTO
{
    public int CarID { get; set; }
    public string Manufacturer { get; set; }
    public List<CarOptionDTO> CarOptions { get; set; }
}

但我无法使用它。 我怎么能这样做呢?

2 个答案:

答案 0 :(得分:0)

您应该使用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();
}

答案 1 :(得分:0)

  1. 您必须在&#39; dataView&#39;上应用排序。并将其重新绑定到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;
    }
    
  2. 或者

    1. 如果你可以使用DataGridView,那就容易多了:
    2. this.myDataGrid.Sort(this.dataGridViewTextBoxColumn1, ListSortDirection.Descending);