Avatar of Jimbo99999
Jimbo99999
Flag for United States of America asked on

Vb.Net - DataGridView Cell Highlighting When Clicked

Good Day Experts!

I have a DataGridView that displays 1 row of data from a SQL table. The row has 4 cells. Only one of the cells is editable.  When the User clicks in the 3 non-editable fields I would like to not have them highlight.  So, only highlight when I click the editable field.

Is this possible to do?

Thanks,
jimbo99999
Visual Basic.NET

Avatar of undefined
Last Comment
Jimbo99999

8/22/2022 - Mon
John (Yiannis) Toutountzoglou

Hi Jimbo...

The row has 4 cells
You mean Column?
Jimbo99999

ASKER
Yes...my  bad column, I was coding quite a bit for Excel earlier in the day.
Ioannis Paraskevopoulos

Hi, set the ReadOnly property of the columns.

For instance:

DataGridView1.Columns(1).ReadOnly = True
DataGridView1.Columns(2).ReadOnly = True
DataGridView1.Columns(3).ReadOnly = True

Open in new window


will allow only column(0) to be editable.

Giannis
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Jimbo99999

ASKER
I have the 3 columns non-editable.  My inquiry was about not having them highlight when I click in them.  I having trying for some time and cannot figure out how to do it.
John (Yiannis) Toutountzoglou

Which Cell of Column 4 is editable ...?
That means that the ohter Cells are ReadOnly.
So for the Cells that are ReadOnlly change The DefaultCellStyle SelectionBackColor and SelectionForeColor

Me.DataGridView1.Columns(4).DefaultCellStyle.SelectionBackColor = Color.White
Me.DataGridView1.Columns(4).DefaultCellStyle.SelectionForeColor = Color.Black

Open in new window


If you give me some more info ??
John (Yiannis) Toutountzoglou

Ok if we are talking about Columns ..

Dim i as integer
For i=1 to 4
Me.DataGridView1.Columns(i).DefaultCellStyle.SelectionBackColor = Color.White
Me.DataGridView1.Columns(i).DefaultCellStyle.SelectionForeColor = Color.Black
Next

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Jimbo99999

ASKER
The DataGridView will only ever show 1 row.  It displays 4 columns.  The first 3 columns are non-editable(Read-only).  The 4th column is the only one that is editable.  I am looking to make the first 3 columns not highlight when I click in them.  When I click in the 4th column, I would want it to still highlight.  

The highlight is to be used as a queue for which column is editable out of the 4.
John (Yiannis) Toutountzoglou

So this is Waht you want ...
Dim i as integer
For i=0 to 3
Me.DataGridView1.Columns(i).DefaultCellStyle.SelectionBackColor = Color.White
Me.DataGridView1.Columns(i).DefaultCellStyle.SelectionForeColor = Color.Black
Next

Open in new window

Also you can make these cells non etitable by "using tab key" like
Private Sub DataGridView1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Tab Then
            Dim currentCell As DataGridViewCell = DataGridView1.CurrentCell
            If currentCell IsNot Nothing Then
                Dim nextRow As Integer = currentCell.RowIndex
                Dim nextCol As Integer = currentCell.ColumnIndex + 1
                If nextCol > DataGridView1.ColumnCount Then
                    nextCol = 0
                    nextRow += 1
                End If
                If nextRow > DataGridView1.RowCount Then
                    nextRow = 0
                End If
                Dim nextCell As DataGridViewCell = DataGridView1.Rows(nextRow).Cells(nextCol)
                If nextCell IsNot Nothing AndAlso nextCell.ReadOnly Then
                    DataGridView1.CurrentCell = nextCell
                End If
            End If
        End If
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
John (Yiannis) Toutountzoglou

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Jimbo99999

ASKER
Thanks for the responses...I will try it now.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Jimbo99999

ASKER
It is working now! Another day, another item for the toolbox.  Thanks again.

jimbo99999