首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Datagridview列单击/排序

Datagridview列单击/排序
EN

Stack Overflow用户
提问于 2013-07-09 08:50:46
回答 2查看 16.9K关注 0票数 2

大家好,大家都知道,如果有人点击列标题,它就会排序。但是,在它们排序之后,是否可以执行某些操作。例如,如果按升序排序,msgbox会提示您按升序排序。我想知道它是哪个数据综合事件。如果可能的话,有没有关于如何判断列是否排序的提示?我找到了这个链接,但它实际上是Web表单,我需要在windows表单中做它,有什么想法吗?

Datagridview column sorting when clicking on the column header

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-09 09:01:25

代码语言:javascript
运行
复制
  Private Sub dataGridView1_ColumnHeaderMouseClick(ByVal sender As Object, _
        ByVal e As DataGridViewCellMouseEventArgs) _
        Handles dataGridView1.ColumnHeaderMouseClick

        Dim newColumn As DataGridViewColumn = _
            dataGridView1.Columns(e.ColumnIndex)
        Dim oldColumn As DataGridViewColumn = dataGridView1.SortedColumn
        Dim direction As ListSortDirection

        ' If oldColumn is null, then the DataGridView is not currently sorted. 
        If oldColumn IsNot Nothing Then 

            ' Sort the same column again, reversing the SortOrder. 
            If oldColumn Is newColumn AndAlso dataGridView1.SortOrder = _
                SortOrder.Ascending Then
                direction = ListSortDirection.Descending
' Msgbox HERE
            Else 

                ' Sort a new column and remove the old SortGlyph.
                direction = ListSortDirection.Ascending
                oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None
               ' Msgbox HERE
            End If 
        Else
            direction = ListSortDirection.Ascending
' Msgbox HERE
        End If 

        ' Sort the selected column.
        dataGridView1.Sort(newColumn, direction)
        If direction = ListSortDirection.Ascending Then
            newColumn.HeaderCell.SortGlyphDirection = SortOrder.Ascending
        Else
            newColumn.HeaderCell.SortGlyphDirection = SortOrder.Descending
        End If 

    End Sub 

    Private Sub dataGridView1_DataBindingComplete(ByVal sender As Object, _
        ByVal e As DataGridViewBindingCompleteEventArgs) _
        Handles dataGridView1.DataBindingComplete

        ' Put each of the columns into programmatic sort mode. 
        For Each column As DataGridViewColumn In dataGridView1.Columns
            column.SortMode = DataGridViewColumnSortMode.Programmatic
        Next 
    End Sub

来源:MSDN

票数 3
EN

Stack Overflow用户

发布于 2013-12-10 21:40:03

代码语言:javascript
运行
复制
  If _Data_Table.Rows.Count > 0 Then
        datagridview.DataSource = _Data_Table


        datagridview.Sort(grd_Cadastro.Columns("Your column name"), System.ComponentModel.ListSortDirection.Descending)


        datagridview.AutoResizeColumns()
        datagridview.ClearSelection()



    Else
        datagridview.DataSource = Nothing
    End If
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17538247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档