【问题标题】:How to retain datagrid ordering asc/desc by column after reload VB.NET重新加载VB.NET后如何按列保留datagrid排序asc/desc
【发布时间】:2012-09-23 18:32:57
【问题描述】:

我在 timer_tick() 子例程中每 60 秒从 Access 数据库填充一次数据网格。

Dim direction As SortOrder
.
.
.

DataGridView1 ColumnHeaderMouse点击代码:

direction = DataGridView1.SortOrder
MsgBox(direction.ToString())

现在,每隔一次点击它就会显示降序和另一半 '2' ,只是数字插入升序。 有谁知道为什么会发生这种情况或如何解决?

我试图实现的是在计时器 tick() 子内自动重新加载数据网格后保留排序顺序。

【问题讨论】:

    标签: vb.net datagrid


    【解决方案1】:

    我找到了答案: http://www.tek-tips.com/viewthread.cfm?qid=1617798

    问题是 DataGridView.SortOrder 返回值 Windows.Forms.SortOrder (此变量类型将检索当前排序顺序)

    同时

    DataGridView.Sort() 正在寻找 System.ComponentModel.ListSortDirection 的值 (此变量类型将设置新的排序顺序)

    所以我们需要这样的东西:

    columnXY = DataGridView1.SortedColumn
    *' if no column set for sort use third one *
            If columnXY Is Nothing Then
                columnXY = DataGridView1.Columns(2)
            End If
    
    Dim SetSortOrder As ListSortDirection
    Dim GridSortOrder As SortOrder
    GridSortOrder = DataGridView1.SortOrder
    
            If GridSortOrder = Windows.Forms.SortOrder.Ascending Then
                SetSortOrder = ListSortDirection.Ascending
            ElseIf GridSortOrder = Windows.Forms.SortOrder.Descending Then
                SetSortOrder = ListSortDirection.Descending
            ElseIf GridSortOrder = Windows.Forms.SortOrder.None Then
                SetSortOrder = ListSortDirection.Ascending
            Else : GridSortOrder = ListSortDirection.Ascending
                MsgBox("not good")
            End If
    
    DataGridView1.DataSource = datasetXY.Tables(0)
    DataGridView1.Sort(columnXY, SetSortOrder)
    

    现在数据网格中的项目将保持按同一列排序,并在重新加载后升序/降序。

    【讨论】:

      猜你喜欢
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-05
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多