如何使用VB.NET WPF按日期对DataGrid中的DataGrid进行排序?

时间:2018-07-05 14:45:29

标签: wpf vb.net sorting datagrid

如何使用VB.NET WPF从数据集中按日期对DataGrid进行排序? Datagrid表应按最新日期自动排列行(Header =“违规日期”)。 我试过使用SortDirection =“ ascending”和SortDirection =“” descending“ 但两者都不起作用。有创意吗?

谢谢。

这是我的UI Datagrid。 Click Here

(XAML)代码

                            <DataGrid x:Name="listCompound" SelectionChanged="listCompound_SelectionChanged"   VerticalGridLinesBrush="Black" Grid.Row ="0" Margin="10,0,0,0" VerticalAlignment="Top" CellStyle="{StaticResource DataGridContentCenter}"  AutoGenerateColumns="False" CanUserAddRows="False"    
                             CanUserSortColumns="True"  IsReadOnly="true" ColumnHeaderStyle="{StaticResource DataGridHeaderFloatingWindow}" HorizontalAlignment="Center" Grid.RowSpan="2">
                                <DataGrid.RowHeaderTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}},Path=Header}" />
                                    </DataTemplate>
                                </DataGrid.RowHeaderTemplate>
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="Compound No." Binding="{Binding Path=NO}"  Width="135">
                                        <DataGridTextColumn.ElementStyle>
                                            <Style TargetType="{x:Type TextBlock}">
                                                <Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
                                                <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                                                <Setter Property="TextBlock.VerticalAlignment"  Value="Center"/>
                                            </Style>
                                        </DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Header="Plate Number" Binding="{Binding Path=VEHICLE}" Width="120" >
                                        <DataGridTextColumn.ElementStyle>
                                            <Style TargetType="{x:Type TextBlock}">
                                                <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                                                <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                                                <Setter Property="TextBlock.VerticalAlignment"  Value="Center"/>
                                            </Style>
                                        </DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Header="Offence Date" Binding="{Binding Path=DATE, StringFormat=dd/MM/yyyy}" Width="90" SortDirection="Descending">
                                        <DataGridTextColumn.ElementStyle>
                                            <Style TargetType="{x:Type TextBlock}">
                                                <Setter Property="TextBlock.VerticalAlignment"  Value="Center"/>
                                            </Style>
                                        </DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>                                        
                                    <DataGridTextColumn Header="Amount" Binding="{Binding Path=PAY}" Width="80" >
                                        <DataGridTextColumn.ElementStyle>
                                            <Style TargetType="{x:Type TextBlock}">
                                                <Setter Property="TextBlock.VerticalAlignment"  Value="Center"/>
                                            </Style>
                                        </DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTemplateColumn Width="100">
                                        <DataGridTemplateColumn.CellTemplate >
                                            <DataTemplate>
                                                <Button x:Name="BtnBayar" Click="BtnBayar_Click" Content="Bayar"  FontWeight="Medium"  BorderBrush="Black"  CommandParameter="{Binding Path=REFNO}" Background="{x:Null}" Width="70" Height="30" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="1" Foreground="Black" Margin="1">
                                                    <Button.Style >
                                                        <Style TargetType="Button" >
                                                            <Style.Triggers>
                                                                <Trigger Property ="IsMouseOver" Value="false">
                                                                    <Setter Property= "Background" Value="Green" />
                                                                </Trigger>
                                                            </Style.Triggers>
                                                        </Style>
                                                    </Button.Style>
                                                </Button>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                </DataGrid.Columns>
                            </DataGrid>

VB.NET

            Dim i As Integer = 0
            While i < CompoundLists.Count
                dsRow = ds.Tables("tblCompound").NewRow
                dsRow("NO") = CompoundLists(i).REFNO
                dsRow("VEHICLE") = CompoundLists(i).VEHICLENO
                dsRow("DATE") = CDate(CompoundLists(i).TRANSDATE)
                dsRow("PAY") = CompoundLists(i).AMNTOPAY
                ds.Tables("tblCompound").Rows.Add(dsRow)
                i += 1

            End While

            TransLog("Storing compound Info in Dataset")
            'txtAccNo.Text = strBillAcc


            dtSource = ds.Tables("tblCompound")

            FillGrid()

1 个答案:

答案 0 :(得分:1)

我是这样做的:

Dim NewView As DataView = ds.DefaultView 'ds is your DataTable right?
NewView.Sort = "DATE DESC"
ds = NewView.ToTable

以此创建数据视图,将数据表复制到它,然后按“ DATE”列降序对其进行排序,然后将数据视图复制到数据表(覆盖)