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

WPF DataGrid Column Sort Direction

$
0
0

Hi,

I have a WPF Data Grid within a user control. This user control is hosted within MainWindow. In the DataGrid, all data are loaded properly and I am able to sort them accordingly. My concern is, initially when the data grid is loaded I want my second column is sorted in Ascending order which is now sorted in Descending order. I specified the SortDirection property for that column but of no use. My code is as follows-

<DataGrid x:Name="DispGrid" ItemsSource="{Binding DispList,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" 

                 Background="White" SelectedItem="{Binding SelectedItem}" ColumnHeaderStyle="{DynamicResource HeaderContainerStyle}" GridLinesVisibility="None"

                 SelectedIndex="0" BorderBrush="Transparent" BorderThickness="0" IsSynchronizedWithCurrentItem="True" HorizontalScrollBarVisibility="Auto"

                 VerticalScrollBarVisibility="Auto" MaxHeight="400" CanUserAddRows="False" MinHeight="300" >

           <DataGrid.RowStyle>

               <Style TargetType="{x:Type DataGridRow}">

                   <Setter Property="FontFamily" Value="Franklin Gothic"/>

                   <Setter Property="FontSize" Value="14"/>

                   <Setter Property="FontWeight" Value="Normal"/>

                   <Setter Property="Foreground" Value="#FF77B6EB"/>

                   <Style.Triggers>

                       <Trigger Property="IsSelected" Value="True">

                           <Setter Property="Foreground" Value="Green"/>

                           <Setter Property="Background" Value="#FFFFFFFF"/>

                       </Trigger>

                       <Trigger Property="IsMouseOver" Value="True">

                           <Setter Property="Foreground" Value="Gray"/>

                           <Setter Property="Background" Value="AliceBlue"/>

                       </Trigger>

                   </Style.Triggers>

               </Style>

           </DataGrid.RowStyle>

           <DataGrid.CellStyle>

               <Style TargetType="{x:Type DataGridCell}">

                   <Setter Property="FontFamily" Value="Franklin Gothic"/>

                   <Setter Property="FontSize" Value="14"/>

                   <Setter Property="FontWeight" Value="Normal"/>

                   <Style.Triggers>

                       <Trigger Property="IsSelected" Value="True">

                           <Setter Property="Foreground" Value="Green"/>

                           <Setter Property="Background" Value="AliceBlue"/>

                           <Setter Property="BorderBrush" Value="Transparent"/>

                           <Setter Property="BorderThickness" Value="0"/>

                       </Trigger>

                   </Style.Triggers>

               </Style>

           </DataGrid.CellStyle>

           <DataGrid.Columns>

               <DataGridTemplateColumn CanUserReorder="False" CanUserResize="False">

                   <DataGridTemplateColumn.HeaderTemplate>

                       <DataTemplate>

                           <CheckBox Name="cbxAll" IsChecked="{Binding DataContext.CheckUncheckAll, Mode=TwoWay,RelativeSource={RelativeSource AncestorType=DataGrid}, UpdateSourceTrigger=PropertyChanged}"

                                     HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center"

                                     Height="16" Margin="0,.5,0,0"/>

                       </DataTemplate>

                   </DataGridTemplateColumn.HeaderTemplate>

                   <DataGridTemplateColumn.CellTemplate>

                       <DataTemplate>

                           <CheckBox Name="chkSeat" IsChecked="{Binding DataContext.IsChecked, Mode=TwoWay,RelativeSource={RelativeSource AncestorType=DataGrid}, UpdateSourceTrigger=PropertyChanged}"

                                     IsThreeState="False" Click="OnAccountClick" />

                       </DataTemplate>

                   </DataGridTemplateColumn.CellTemplate>

               </DataGridTemplateColumn>

               <DataGridTextColumn Header="Disp ID" Binding="{Binding Path=DispModel.DispId}"SortDirection="Ascending" Width="100"/>

               <DataGridTextColumn Header="Category Name" Binding="{Binding Path=DispensationModel.CategoryName}" Width="90"/>

               <DataGridTextColumn Header="Status" Binding="{Binding Path=DispensationModel.Status}" Width="110"/>

           </DataGrid.Columns>

       </DataGrid>

I want this DispID Column is sorted in Ascending order when the grid is loaded for the first time.

Any suggestion is Welcome.

Thanks


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>