Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataGrid sorting example has duplicate 'e.Column.Tag.ToString() == "Range"' check #702

Open
Trolldemorted opened this issue Jan 7, 2024 · 0 comments

Comments

@Trolldemorted
Copy link

Trolldemorted commented Jan 7, 2024

This is redundant, right?

private void dg_Sorting(object sender, DataGridColumnEventArgs e)
{
    //Use the Tag property to pass the bound column name for the sorting implementation 
    if (e.Column.Tag.ToString() == "Range")
    {
        //Use the Tag property to pass the bound column name for the sorting implementation
        if (e.Column.Tag.ToString() == "Range")
        {
            //Implement sort on the column "Range" using LINQ
            if (e.Column.SortDirection == null || e.Column.SortDirection == DataGridSortDirection.Descending)
            {
                dg.ItemsSource = new ObservableCollection<Mountain>(from item in _items
                                                                    orderby item.Range ascending
                                                                    select item);
                e.Column.SortDirection = DataGridSortDirection.Ascending;
            }
            else
            {
                dg.ItemsSource = new ObservableCollection<Mountain>(from item in _items
                                                                    orderby item.Range descending
                                                                    select item);
                e.Column.SortDirection = DataGridSortDirection.Descending;
            }
        }
        // add code to handle sorting by other columns as required

        // Remove sorting indicators from other columns
        foreach (var dgColumn in dg.Columns)
        {
            if (dgColumn.Tag.ToString() != e.Column.Tag.ToString())
            {
                dgColumn.SortDirection = null;
            }
        }
    }
}

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Tasks

No tasks being tracked yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant