【问题标题】:How to: Sort items of a DataGrid如何:对 DataGrid 的项目进行排序
【发布时间】:2014-07-11 11:28:49
【问题描述】:

如何:使用 C#/WPF 对 DataGrid 的项目进行排序

我确实有以下代码sn-ps(不重要的代码已被删除):

C#:

lastName.SortDirection = ListSortDirection.Ascending;

XAML:

<DataGrid AutoGenerateColumns="False" Name="dataGrid_Content">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding lastName}" Header="Nachname" x:Name="lastName" />
    </DataGrid.Columns>
</DataGrid>

不幸的是,C# 代码被忽略了 - 没有升序排序,它只创建显示的小箭头,但项目没有排序。我的错误是什么?

编辑我:

public void SetItemsToDataContext()
    {
        dataGrid_Content.Items.Clear();

        foreach (string s in Directory.GetFiles(@"C:\Users\...", "*.txt"))
        {
            StreamReader streamReader = new StreamReader(s);

            int i = 1;
            string line = streamReader.ReadToEnd().Replace("\n", "");
            string[] t = line.Split('\r');

            BusinessContact businessContact = new BusinessContact();
            businessContact.firstName = t[i + 2];
            businessContact.lastName = t[i + 3];

            dataGrid_Content.Items.Add(businessContact);

            streamReader.Close();
        }

        applySortDescriptions(lastName, ListSortDirection.Ascending);
    }

编辑二:

public string getSortPropertyName(DataGridColumn col)
{
    return "Content";
}

【问题讨论】:

  • 尝试使用 CanUserSort = "True"。让我们知道它是否适合您。
  • 你的意思是CanUserSortColumns="True"?我这样做了,但它没有帮助,对不起。它不工作。

标签: c# xaml sorting datagrid sortdirection


【解决方案1】:

嗯,有一种方法可以让它正常工作。在这里。

    private void applySortDescriptions(DataGridColumn col, ListSortDirection listSortDirection)
    {
        //Clear current sort descriptions
        MyDataGrid.Items.SortDescriptions.Clear();

        //Get property name to apply sort based on desired column
        string propertyName = getSortPropertyName(col);           

        //Add the new sort description
        MyDataGrid.Items.SortDescriptions.Add(new SortDescription(propertyName, listSortDirection));

        //apply sort
        applySortDirection(col, listSortDirection);           

        //refresh items to display sort
        MyDataGrid.Items.Refresh();
    }

    private string getSortPropertyName(DataGridColumn col)
    {
        //place logic in here that will return the name of the property to sort by (ex: return “name”; if you are sorting by the name property)

        return string.Empty;
    }

    private void applySortDirection(DataGridColumn col, ListSortDirection listSortDirection)
    {
        foreach (DataGridColumn c in PatientsViewDatGrid.Columns)
        {
            c.SortDirection = null;
        }
        col.SortDirection = listSortDirection;
    }

应该这样做。现在您可以进行排序,列标题将适当地显示排序指示符

【讨论】:

  • 谢谢你,它似乎工作。让我再检查一次。 ;)
  • 它是datagridview的名称。你可以用你的替换它。
  • 您是否通过传递要排序的datagridview 的Column 来调用applySortDescriptions() 方法?
  • 是的,我是这样做的:applySortDescriptions(lastName, ListSortDirection.Ascending);。但它没有正确排序。它以这种方式排序:B1,A1,M1。 --> 但应该是 A1, B1, M1。
  • 您必须将 Datagridview 列作为参数传递。
猜你喜欢
  • 1970-01-01
  • 2013-06-08
  • 1970-01-01
  • 1970-01-01
  • 2014-12-12
  • 2012-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多