Vb.net 将数据绑定到网格后,排序箭头消失,列标题看起来像';再残疾
这可能是一个非常基本的问题…但找不到答案 我正在使用VB.NET开发我的windows应用程序。我正在将集合绑定到Datagridview。我只添加了一列,正在尝试对其进行排序。在绑定之前,数据网格视图如下图所示 在上图中,我们可以看到网格中的箭头和* 在我绑定数据后,它就消失了,所以我无法像下图那样对任何列进行排序 这是代码Vb.net 将数据绑定到网格后,排序箭头消失,列标题看起来像';再残疾,vb.net,datagridview,Vb.net,Datagridview,这可能是一个非常基本的问题…但找不到答案 我正在使用VB.NET开发我的windows应用程序。我正在将集合绑定到Datagridview。我只添加了一列,正在尝试对其进行排序。在绑定之前,数据网格视图如下图所示 在上图中,我们可以看到网格中的箭头和* 在我绑定数据后,它就消失了,所以我无法像下图那样对任何列进行排序 这是代码 Try Me.Cursor = Cursors.WaitCursor gvBatchList.AutoGenerat
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
将源绑定到datagridview后,为要排序的列设置排序模式 以下是一个例子:
For Each col As DataGridViewColumn In myGrid.Columns
col.SortMode = DataGridViewColumnSortMode.Programmatic
Next
这些选项不可排序、自动和编程。自动将允许通过标准方式进行排序,而编程将允许您指定排序时的行为。尝试“自动”,如果它的行为不符合您的要求,请执行编程
以下是我的排序方法示例:
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
Private Sub-myGrid\u排序(发送方作为对象,e作为DataGridViewCellMouseEventArgs)
Cursor=Cursors.WaitCursor
如果myGrid.SortedColumn不是空的,那么
'如果是不同的排序列,请按升序排序,并将以前排序的列更改为无
如果e.ColumnIndex myGrid.SortedColumn.Index,则
对于col=0到myGrid.Columns.Count-1
myGrid.Columns(col).HeaderCell.SortGlyphDirection=SortOrder.None
下一个
myGrid.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection=SortOrder.Ascending
myGrid.Sort(myGrid.Columns(e.ColumnIndex)、ListSortDirection.Ascending)
否则,切换排序的方向
如果myGrid.SortOrder=SortOrder.singressing,则
myGrid.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection=SortOrder.Descending
myGrid.Sort(myGrid.Columns(e.ColumnIndex)、ListSortDirection.Descending)
其他的
myGrid.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection=SortOrder.Ascending
myGrid.Sort(myGrid.Columns(e.ColumnIndex)、ListSortDirection.Ascending)
如果结束
如果结束
Else'如果网格尚未排序,只需按所选列升序排序即可
myGrid.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection=SortOrder.Ascending
myGrid.Sort(myGrid.Columns(e.ColumnIndex)、ListSortDirection.Ascending)
如果结束
游标=游标。默认值
端接头
给我们一些您需要的代码wrote@user1650894在设置数据源后,您是否尝试过设置DataGridViewColumnSortMode
。
在绑定(数据源)后尝试设置此选项:DataGridViewColumnSortMode。为该列编程。如果您给我一些示例,那将非常好…谢谢,我将尝试它,但是在上面的代码(对于每个循环…代码)中,我应该在哪个事件中写入?@user1650894-设置排序模式的循环在绑定源之后的某个时间与将数据源绑定到datagridview的事件相同。