Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

SortDirection coming as Null for datagrid

$
0
0

DataGrid sort direction is coming as null. Why is sort direction coming as null during the first click on the column? My intention is to save the sort. But if sort direction itself is coming as null how will I save it?

Please advise

See the sample code below:-

  public partial class MainWindow : Window
    {
        DataTable dt = new DataTable();
        public DataTable MyDataTable
        {
            get { return dt; }
        }

        public MainWindow()
        {
            InitializeComponent();

            DataContext = new List<Person>
            {
                new Person {Name="Tom", Age=10}, 
                new Person {Name="Ken", Age=20}, 
                new Person {Name="Jen", Age=30}, 
            };
        }

        private void testGrid_Sorting(object sender, DataGridSortingEventArgs e)
        {
            if (e.Column.SortDirection == null)
                MessageBox.Show("Sort Direction is Null"); // This is happening during the first click on a column
        }
    }

    public class Person
    {
        public string Name { set; get; }
        public int Age { set; get; }
    }
}

Xaml

 <Grid>
        <DataGrid Name="testGrid"  SelectionMode="Single" ItemsSource="{Binding}" AutoGenerateColumns="True" Width=" 300" Height="300" Sorting="testGrid_Sorting" />
    </Grid>




Viewing all articles
Browse latest Browse all 18858

Trending Articles