winforms 在运行时对列进行排序时,DataGridView行标题编号消失

ruyhziif  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(262)

我有一个DataGridView控件,我已经将它的DataGridViewRow.HeaderCell.Value设置为一个整数。行的编号为1, 2, 3 ...(这可能与此无关,无论我将值设置为什么,我都会遇到相同的行为)。
在运行时,当我单击Column的标题以切换排序顺序时,行标题消失。更准确地说,分配给标题的数值被清除。
我可以在For Each循环中轻松地将每个DataGridViewColumnSortMode属性设置为NotSortable,但我希望保留排序功能。
有办法处理吗?
下面是我的代码:

Imports System.IO
Imports System.Data.SqlClient

Public Class Form1

    Dim CMD As New SqlCommand()
    Dim ADP As New SqlDataAdapter()
    Dim TBL As New DataTable

    Dim serverStr As String = "Server"
    Dim databaseStr As String = "Database"
    Dim dbLoginStr As String = "Login"
    Dim dbPassStr As String = "Password"

    Dim qry As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim connectionString As String =
            "Server=" & serverStr & ";" &
            "Database=" & databaseStr & ";" &
            "User Id=" & dbLoginStr & ";" &
            "Password=" & dbPassStr & ";" &
            "Timeout=1"

        qry = "SELECT * FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME ASC"

        Try
            Using SQL As New SqlConnection
                SQL.ConnectionString = connectionString

                CMD.Connection = SQL
                CMD.CommandText = qry
                ADP.SelectCommand = CMD

                SQL.Open()
                ADP.SelectCommand = CMD
                TBL.Clear()
                TBL.Columns.Clear()
                ADP.Fill(TBL)
                SQL.Close()
            End Using
        Catch ex As Exception
            MsgBox("Could not connect to " & databaseStr & " on server: " & serverStr & vbNewLine & vbNewLine & ex.Message)
            Exit Sub
        End Try

        DataGridView1.DataSource = TBL
        DataGridView1.AutoResizeColumns()
        SetRowNumber(DataGridView1)
        DataGridView1.Columns(DataGridView1.Columns.Count - 1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    End Sub

    Private Sub SetRowNumber(myDGV As DataGridView)
        For Each row As DataGridViewRow In myDGV.Rows
            row.HeaderCell.Value = (row.Index + 1).ToString()
        Next
        myDGV.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)

        'Remove Sorting ability
        'For Each col As DataGridViewColumn In myDGV.Columns
        '    col.SortMode = DataGridViewColumnSortMode.NotSortable
        'Next
    End Sub

    'See if value got cleared upon sorting
    'Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick

    '    Dim myDGV = DirectCast(sender, DataGridView)

    '    For Each row As DataGridViewRow In myDGV.Rows
    '        MsgBox(row.HeaderCell.Value)
    '    Next
    'End Sub
End Class
u0sqgete

u0sqgete1#

您可以使用RowPostPaint事件在标题中绘制行号。
每次需要重新绘制Row时都会引发此事件,因此每次需要时都会重新绘制数字,并保持不变。
您可能需要设置RowHeadersWidthSizeMode属性:

[DataGridView].RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing

这将防止用户调整行标题的大小,弄乱标题的 label(嗯,数字将只是隐藏/部分可见。尽管如此,这是一个选项)。
数字是右对齐和垂直居中绘制的,所以数字会在左边展开。您可以更改此行为,将StringFormatStringAlignment从**Far(右)更改为Near**(左):

[StringFormat].Alignment = StringAlignment.Far   ' Align right
 [StringFormat].Alignment = StringAlignment.Near  ' Align left

数字的颜色设置为DataGridView的**dgv.RowTemplate.DefaultCellStyle.ForeColor
更改此默认样式以设置其他颜色。
字体设置为DataGridView的
DefaultCellStyle.Font。考虑事项相同。
数字本身的格式为
$"{(e.RowIndex + 1):00}"**。请根据需要进行更改。

Private Sub DataGridView1_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
    Dim dgv = DirectCast(sender, DataGridView)
    Dim rowHeader = $"{(e.RowIndex + 1):00}"

    Dim headerBounds As Rectangle =
        New Rectangle(e.RowBounds.Left, e.RowBounds.Top, dgv.RowHeadersWidth - 6, e.RowBounds.Height)

    Using format = New StringFormat(StringFormatFlags.NoWrap)
        format.Alignment = StringAlignment.Far
        format.LineAlignment = StringAlignment.Center
        Using brush As SolidBrush = New SolidBrush(dgv.RowTemplate.DefaultCellStyle.ForeColor)
            e.Graphics.DrawString(rowHeader, dgv.DefaultCellStyle.Font, brush, headerBounds, format)
        End Using
    End Using
End Sub

相关问题