DataGridView without selected row at the beginning

后端 未结 12 1040
栀梦
栀梦 2021-02-12 14:23

In my WinForms I have DataGridView. I wanted to select full row at once so I set SelectionMode as FullRowSelect. And now I have problem, b

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-12 14:59

    Sometimes, when you reload your form without closing your program, the first row will be highlighted. But it will not be selected, and you will get -1 for selected row index.

    You can do it just like this:

     1. Store default styles when the form is loading:

     Public Class aRoots
        Dim df1, df2, df3, df4 As Color
        Private Sub aRoots_Load(sender As Object, e As EventArgs) Handles Me.Load
                df1 = DGV_Root.DefaultCellStyle.SelectionBackColor
                df2 = DGV_Root.DefaultCellStyle.BackColor
                df3 = DGV_Root.DefaultCellStyle.SelectionForeColor
                df4 = DGV_Root.DefaultCellStyle.ForeColor
    

     2. Change cell styles when interacting with datagridview:

    Private Sub LoadRoot()
           For i = 0 To 5
                    DGV_Root.Rows.Add()
                    For j = 0 To 3
                        DGV_Root.Item(j, i).Value = ...
                    Next
                Next
            'DGV_Root.ClearSelection() ==> instead of this use 2 lines below
            DGV_Root.DefaultCellStyle.SelectionBackColor = df2
            DGV_Root.DefaultCellStyle.SelectionForeColor = df4
        End Sub
    

     3. Change cell styles to default when selection is being changed like cell_click or cell_double click:

    Private Sub DGV_Root_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DGV_Root.CellMouseClick
            DGV_Root.DefaultCellStyle.SelectionBackColor = df1
            DGV_Root.DefaultCellStyle.SelectionForeColor = df3
    
    
    ...
    End Sub
    

     4. restore all to default when u want to close form:

    Private Sub PbClose_Click(sender As Object, e As EventArgs) Handles PbClose.Click
            BtnCancel.PerformClick()
            DGV_Root.DefaultCellStyle.SelectionBackColor = df1
            DGV_Root.DefaultCellStyle.BackColor = df2
            DGV_Root.DefaultCellStyle.SelectionForeColor = df3
            DGV_Root.DefaultCellStyle.ForeColor = df4
            Me.Close()
    End Sub
    

    Hope this help you guys.

提交回复
热议问题