Link to home
Start Free TrialLog in
Avatar of Jimbo99999
Jimbo99999Flag 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
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Hi Jimbo...

The row has 4 cells
You mean Column?
Avatar of Jimbo99999

ASKER

Yes...my  bad column, I was coding quite a bit for Excel earlier in the day.
Avatar of 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
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.
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 ??
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

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.
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
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the responses...I will try it now.
It is working now! Another day, another item for the toolbox.  Thanks again.

jimbo99999