【问题标题】:How to sort the column in datagrid view ? which events involved in it?如何对数据网格视图中的列进行排序?它涉及哪些事件?
【发布时间】:2016-03-25 03:23:18
【问题描述】:

我正在 vb.net 中开发 windows 应用程序。

我有几个列的数据网格,我想对我选择的列进行排序。

如下图所示,我已经选择了 ID 列,当我点击排序按钮时,它应该得到升序或降序排序。

(现在我不担心顺序,我对代码更感兴趣。)

我检查了几个关于排序问题的问题,但没有人回答其中涉及哪些事件,他们只给出了一个代码。

到目前为止,我只能编写下面的代码来选择列,但这不起作用,因为我在代码中编写了硬编码的 0 列索引。

如何编写选择列和排序的代码?

 Private Sub gvBatchList_ColumnHeaderMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles gvBatchList.ColumnHeaderMouseClick

        For Each c As DataGridViewColumn In gvBatchList.Columns
            c.SortMode = DataGridViewColumnSortMode.NotSortable
            c.Selected = False
        Next
        gvBatchList.SelectionMode = DataGridViewSelectionMode.FullColumnSelect
        gvBatchList.Columns(0).Selected = True
    End Sub

提示:我使用 Access 数据库来存储记录并使用集合,同时将记录绑定到网格视图。

下面的代码展示了我是如何绑定记录的......

 Private Sub LoadAllRegularEmployees()
        Try
            Me.Cursor = Cursors.WaitCursor
            DataGridView1.AutoGenerateColumns = False
            Dim oEmployees As New Employees
            oEmployees.LoadAllRegularEmployees()
            DataGridView1.DataSource = oEmployees
            lblEmployeesCount.Text = "Toal Employees (" + oEmployees.Count().ToString + ")"
            txtEmployeeSearch.Focus()
            Me.Cursor = Cursors.Arrow
        Catch ex As Exception
            Me.Cursor = Cursors.Arrow
            MessageBox.Show("Error :- " + ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

【问题讨论】:

  • 是否将列的SortMode 的属性设置为Automatic 不是一个选项?然后用户只需点击列标题即可进行排序。
  • 这个问题应该使用datagridview的标签而不是datagrid。 OP 还应该提供更多信息,至少他正在使用什么样的数据源。

标签: vb.net sorting datagrid


【解决方案1】:

编辑:

如果一开始你没有成功……等等。

Select Case sortDescending
    Case True
        gvBatchList.Sort(gvBatchList.SelectedColumns(0), ListSortDirection.Ascending)
        sortDescending = False
    Case Else
        gvBatchList.Sort(gvBatchList.SelectedColumns(0), ListSortDirection.Descending)
        sortDescending = True
End Select

在这里,我定义了一个布尔值sortDescending,并用它来检查我们的排序方式。我将其初始化为False,但如果您希望它先升序排序,您只需将其初始化为True

您还必须导入 System.ComponentModel 才能使用 ListSortDirection

希望能做到!


我想这就是你想要的:

gvBatchList.Sort(gvBatchList.SelectedColumns(0), SortOrder.Ascending)

或者,您可以使用SortOrder.Descending 向另一个方向排序。也许是这样的?

Select Case gvBatchList.SelectedColumns(0).SortMode
    Case SortOrder.Descending
        gvBatchList.Sort(gvBatchList.SelectedColumns(0), SortOrder.Ascending)
    Case Else
        gvBatchList.Sort(gvBatchList.SelectedColumns(0), SortOrder.Descending)
End Select

如果您的用户没有选择列,我无法保证会发生什么。

【讨论】:

  • 我已经尝试过,但它给出了一个错误:- mscorlib.dll 中发生了“System.ArgumentOutOfRangeException”类型的未处理异常附加信息:索引超出范围。必须是非负数且小于集合的大小。选择一列是主要事件,该怎么做?
  • 在我们深入探讨之前,请参阅我对您问题的评论。我们可以将列的SortMode 设置为Automatic,还是有不能这样做的原因?
  • 是的,我们可以将其设置为芳香,但如何做到这一点?
  • 您使用的是 Visual Studio,还是需要以编程方式进行?编辑:无论哪种方式,都有一个问题。您不能启用全列选择使用自动排序模式。
  • 是的,我正在使用 Visual Studio。我很困惑......我到底想做什么?我可以接受任何选择...只需进行排序...
【解决方案2】:

objSort = objSort.OrderBy(Function(x) x.Assembly).ToArray()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 2011-12-30
    • 2013-04-22
    相关资源
    最近更新 更多