【问题标题】:After binding the data to grid, sorting arrow disappears, column headers look like they're disabled将数据绑定到网格后,排序箭头消失,列标题看起来像是被禁用了
【发布时间】:2015-12-22 09:50:17
【问题描述】:

这可能是一个非常基本的问题...但找不到答案。

我正在使用 VB.NET 开发我的 Windows 应用程序。我将集合绑定到 Datagridview。我只添加了一列并尝试对其进行排序。在绑定之前,数据网格视图如下图所示。

在上图中,我们可以看到网格中的箭头和 *...

绑定数据后,它就消失了,因此我无法对任何列进行排序,如下图所示。

这里是代码...

 Try
            Me.Cursor = Cursors.WaitCursor
            gvBatchList.AutoGenerateColumns = False
            Dim oBatchCollection As New Batches
            oBatchCollection.LoadOngoingBatches(True)
            gvBatchList.DataSource = oBatchCollection
            lblBatchCount.Text = "Batches (" + oBatchCollection.Count().ToString + ")"
            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

【问题讨论】:

  • 给我们一些你写的代码
  • @user1650894 您是否尝试在设置DataSource. 后设置DataGridViewColumnSortMode 在您绑定(数据源)后尝试设置:DataGridViewColumnSortMode.Programmatic 为该列。
  • 如果你能给我一些例子就好了......

标签: vb.net datagridview


【解决方案1】:

将源绑定到datagridview后,为要排序的列设置排序模式。

这是一个例子:

    For Each col As DataGridViewColumn In myGrid.Columns
        col.SortMode = DataGridViewColumnSortMode.Programmatic
    Next

选项包括不可排序、自动和程序化。 Automatic 将允许按标准方式排序,而 Programmatic 将允许您指定排序时的行为。尝试自动,如果它的行为不符合您的要求,请执行程序化。

这是我的排序方法示例:

Private Sub myGrid_Sorting(sender As Object, e As DataGridViewCellMouseEventArgs)
    Cursor = Cursors.WaitCursor

    If myGrid.SortedColumn IsNot Nothing Then
        'If a different sort column, sort ascending and change previously sorted column to none
        If e.ColumnIndex <> myGrid.SortedColumn.Index Then
            For col = 0 To myGrid.Columns.Count - 1
                myGrid.Columns(col).HeaderCell.SortGlyphDirection = SortOrder.None
            Next
            myGrid.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection = SortOrder.Ascending
            myGrid.Sort(myGrid.Columns(e.ColumnIndex), ListSortDirection.Ascending)
        Else  'otherwise toggle the direction of the sort
            If myGrid.SortOrder = SortOrder.Ascending Then
                myGrid.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection = SortOrder.Descending
                myGrid.Sort(myGrid.Columns(e.ColumnIndex), ListSortDirection.Descending)
            Else
                myGrid.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection = SortOrder.Ascending
                myGrid.Sort(myGrid.Columns(e.ColumnIndex), ListSortDirection.Ascending)
            End If
        End If
    Else  'if grid is not already sorted, simply sort by selected column in ascending order
        myGrid.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection = SortOrder.Ascending
        myGrid.Sort(myGrid.Columns(e.ColumnIndex), ListSortDirection.Ascending)
    End If

    Cursor = Cursors.Default
End Sub

【讨论】:

  • 谢谢,我会试试的,但是在上面的代码(对于每个循环...代码)的哪个事件中我应该写?
  • @user1650894 - 设置排序模式的循环与将数据源绑定到 datagridview 的事件发生在同一事件中......只是在绑定源之后的某个时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-06
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 2015-07-31
  • 2021-05-08
  • 2016-11-13
相关资源
最近更新 更多