如何以编程方式对datagridview列进行排序

时间:2016-01-05 20:35:25

标签: vb.net sorting datagridview

我被要求在VB.Net创建的DataGridView上对列进行排序。这不是我的代码,但我试图提供帮助。以下是代码的一部分:

Try
        Dim sqlSelect As String = "SELECT  * FROM Manpower WHERE LogOutTime IS NULL AND LogInDate = #" & dateToday & "# ORDER BY CustomerName"

        Dim myDataAdapter = New OleDbDataAdapter(sqlSelect, myWorkforceConnection)

        myCommandBuilder = New OleDbCommandBuilder(myDataAdapter)

        myDataAdapter.Fill(myDataTable)

    Catch ex As Exception
        MsgBox(ex.Message)

        Exit Sub
    End Try

    If myDataTable.Rows.Count = 0 Then
        MessageBox.Show("No records found.")
        Exit Sub
    End If

    dgvManpower.DataSource = myDataTabl

    dgvManpower.Columns("ID").Visible = False

    dgvManpower.Columns("EmployeeName").Width = 175
    dgvManpower.Columns("EmployeeName").SortMode = DataGridViewColumnSortMode.Automatic
    dgvManpower.Columns("EmployeeName").HeaderText = "Employee Name"

当我运行应用程序时,我无法对EmployeeName列进行排序。 Microsoft文档声称字形将添加到列标题但不会出现。如何让专栏成为" sort-able" ?

1 个答案:

答案 0 :(得分:0)

我在代码中看不到任何会停止排序的内容。

检查IDE中的datagridview设置,并确保datagridview具有Enabled=TrueColumnHeadersVisible=True

然后点击列标题,看看会发生什么。