DataGridView cell selection color

amnon

Member
Joined
Dec 18, 2022
Messages
16
Programming Experience
Beginner
Hello,
In DataGridView, I set up FullRowSelect., after clicking on a cell, I would like it to change color to white, and I managed to do that, but after the next click on another cell, the previously clicked cell is still white. I don't know how to get rid of the previous selection.


VB.NET:
Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick

        Try
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.SelectionBackColor = Color.White
        Catch ex As Exception
            Exit Try
        End Try

    End Sub
 
In FullRowSelect all row cells are selected.
 
You could create a form level variable that contains the "address" of the last selected cell, then flip the color of the previously selected cell, set the color of the newly selected cell and save the new cell address to the variable.

EDIT:
Actually, just a static var in the click event will handle it:

VB.NET:
         Static lastSelectedCell As String

        If lastSelectedCell Is Nothing Then lastSelectedCell = ""

        Try
            If lastSelectedCell.Length() > 0 Then
                ' reset the previously selected cell's background
                DataGridView1.Rows(lastSelectedCell.Split(",")(0)).Cells(lastSelectedCell.Split(",")(1)).Style.SelectionBackColor = Color.Blue

            End If
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.SelectionBackColor = Color.White
            lastSelectedCell = e.RowIndex.ToString & "," & e.ColumnIndex  ' remember this selection

        Catch ex As Exception
            Exit Try
        End Try

edit 2: code formatted
 
Last edited:
Thank's jdelano, one thing had to be changed

was:
VB.NET:
 DataGridView1.Rows(lastSelectedCell.Split(",")(0)).Cells(lastSelectedCell.Split(",")(1)).Style.SelectionBackColor = Color.Blue

changed to:
VB.NET:
DataGridView1.Rows(CType(lastSelectedCell.Split(",")(0), Int32)).Cells(CType(lastSelectedCell.Split(",")(1), Int32)).Style.SelectionBackColor = Color.Blue

and it works great, but there's one more thing I can't deal with, how to change Color.Blue to the color of selected row. I read on some forum that it is related to how I set the colors in DataGridView.
My color settings:
.RowsDefaultCellStyle.BackColor .AlternatingRowsDefaultCellStyle.BackColor.
 
For the record, if you're using CType for an intrinsic type, you can use the shorter version, e.g. CStr(x) instead of CType(x, String) and CInt(x) instead of CType(x, Integer). That will make your code more readable.
 
how to change Color.Blue to the color of selected row. I read on some forum that it is related to how I set the colors in DataGridView.
My color settings:
.RowsDefaultCellStyle.BackColor .AlternatingRowsDefaultCellStyle.BackColor.
Do you mean reset the color back to the color it was before selecting the cell so that it matches the default / alternate color it had before selecting the cell, or when you select a cell (it turns blue) you still need it to be the same color as the row it is on? If so, how would you know it is selected?

EDIT: I had selected to quote but it didn't, so I inserted it.
 
hej jdelano,

I just want the one cell I clicked on, to have the color I gave the row earlier (using
DatagridView.RowsDefaultCellStyle.BackColor and DatagridView.AlternatingRowsDefaultCellStyle.BackColor) so I need to check what color this cell had earlier.
 

Latest posts

Back
Top