Show row number in row header of a DataGridView

前端 未结 11 2376
野性不改
野性不改 2020-12-01 06:17

Is it possible to show row number in the row header of a DataGridView?

I\'m trying with this code, but it doesn\'t work:

    pri         


        
11条回答
  •  天涯浪人
    2020-12-01 06:56

    This worked for me.

    Private Sub GridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles GridView1.CellFormatting
        Dim idx As Integer = e.RowIndex
        Dim row As DataGridViewRow = VDataGridView1.Rows(idx)
        Dim newNo As Long = idx
        If Not _RowNumberStartFromZero Then
            newNo += 1
        End If
    
        Dim oldNo As Long = -1
        If row.HeaderCell.Value IsNot Nothing Then
            If IsNumeric(row.HeaderCell.Value) Then
                oldNo = CLng(row.HeaderCell.Value)
            End If
        End If
    
        If newNo <> oldNo Then 'only change if it's wrong or not set
            row.HeaderCell.Value = newNo.ToString()
            row.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
        End If
    End Sub
    

提交回复
热议问题