如何在DataGrid

时间:2016-04-20 18:44:56

标签: c# wpf vb.net sorting datagrid

我有一个带字母数字值的DataGrid,例如:

  • 1秒
  • 100英里
  • 1499
  • 28th street
  • 50个苹果
  • 701:素数
  • 9
  • 香蕉

这就是它自动调整的方式,因为所有值都是字符串。如何将其排序为整数,如下所示:

  • 1秒
  • 9
  • 28th street
  • 50个苹果
  • 100英里
  • 701:素数
  • 1499
  • 香蕉

我已尝试使用SortMemberPath,但我不确定如何在我的代码隐藏中实现此功能。

<DataGrid.Columns>
    <DataGridTextColumn x:Name="IdColumn" Binding="{Binding Id}" Header="Id"/>
    <DataGridTextColumn x:Name="NameColumn" Binding="{Binding Name}" Header="Name" SortMemberPath="NumSort"/>
</DataGrid.Columns>

我甚至尝试过像this这样的实现,但我尝试排序的属性是自动生成的代码(ItemsDataSet.Designer.vb),超过10000行。不确定在那里放置任何东西是否聪明,但我仍然尝试过,如下:

<Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")>  _
Public Property Name() As String
    Get
        Return CType(Me(Me.tablet_Items.NameColumn),String)
    End Get
    Set
        Me(Me.tablet_Items.NameColumn) = Value
        If Value IsNot Nothing Then NumSort = Integer.Parse(Value.ToString())
    End Set
End Property

然而,VS抱怨NumSort没有被宣布。我不确定在代码中将其声明的地方,我曾在多个地方尝试过,但没有运气。

编辑:

我向前迈出了一小步。我已经实现了这段代码,允许我对两列进行排序,但我仍然不确定如何访问网格中的每条记录进行一些比较。

Private Sub dataGridName_Sorting(sender As Object, e As DataGridSortingEventArgs) Handles T_MoviesDataGrid.Sorting
    e.Handled = True
    Dim cView = CollectionViewSource.GetDefaultView(sender.ItemsSource)
    Dim direction As ListSortDirection = ListSortDirection.Ascending
    If cView.SortDescriptions.FirstOrDefault().PropertyName = e.Column.SortMemberPath Then
        direction = If(cView.SortDescriptions.FirstOrDefault().Direction = ListSortDirection.Descending, ListSortDirection.Ascending, ListSortDirection.Descending)
    End If

    cView.SortDescriptions.Clear()
    If e.Column.SortMemberPath = "NameSort" Then
        AddSortColumn(DirectCast(sender, DataGrid), "Name", direction)
        AddSortColumn(DirectCast(sender, DataGrid), "Id", direction)
    End If
End Sub

Private Sub AddSortColumn(sender As DataGrid, sortColumn As String, direction As ListSortDirection)
    Dim cView = CollectionViewSource.GetDefaultView(sender.ItemsSource)
    cView.SortDescriptions.Add(New SortDescription(sortColumn, direction))
    For Each col In sender.Columns.Where(Function(x) x.SortMemberPath = sortColumn)
        col.SortDirection = direction
    Next
End Sub

在一些类似的主题中,人们经常建议使用IComparer。我试图实施,但我不知道如何实现。这就是我被困的地方:

Public Class MyComparing
    Inherits MyDataSet
    Implements IComparable
    Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
        Return '???
    End Function
End Class

我没有继承MyDataSet,而是尝试使用partial,但仍然卡住了。

2 个答案:

答案 0 :(得分:0)

试试这个

  • Dim View As New DataView(tbl)

  • View.Sort =“Scol1,Scol2”

  • View.Table = DataSet1.Tables(“dtBills”)

  • 第二个选项是正则表达式以精简数字 直到第一次出现任何字符为例,取“123” “123Abc”并从以下函数返回

_ Public Property Name() As String Get Return CType(Me(Me.tablet_Items.NameColumn),String) End Get Set Me(Me.tablet_Items.NameColumn) = Value int yourIndexNumber = str.IndexOf(char)(c => !char.IsWhiteSpace(c)); If Value IsNot Nothing Then NumSort = int.Parse(value.Substring(0, value.IndexOf(yourIndexNumber,StringComparison.InvariantCultureIgnoreCase))) End Set End Property

答案 1 :(得分:0)

在带有数据网格的xaml文件的代码中,您可以创建自定义排序规则NumSort

将NumSort应用于datagrid排序事件,如下所示:

AddHandler dataGrid.Sorting, AddressOf NumSort

或者等同于

的vb
dataGrid.Sorting += new DataGridSortingEventHandler(NumSort)

这在答案中进一步描述: How can I apply a custom sort rule to a WPF DataGrid?