We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How can I highlight the current cell in DataGrid?

Can someone please tell me how to highlight the current cell with other color in a DataGrid of Windows Form?

4 Replies

CB Clay Burch Syncfusion Team July 18, 2002 06:10 AM

> Can someone please tell me how to highlight the current cell with other color in a DataGrid of Windows Form? What do you mean by highlighting? Normally, the current cell has edit focus which means the highlighting of text in a TextBox. Is it that you want to change the highlighting color of this text box? Or, do you never want to see an active edit cell, but just show the square where the user clicks in some different color?

RU rufus July 18, 2002 02:48 PM

I mean to change the background of current cell with another color beside "grey" since the default color of active cell is ugly enough when the dataGrid is set to disable.

CB Clay Burch Syncfusion Team July 21, 2002 03:25 AM

You mean 'ReadOnly' when you said disabled, correct? If so, you can use the first method from this FAQ. How do I color a individual cell depending upon its value or some external method? http://www.syncfusion.com/faq/winforms/search/745.asp Instead of the derived columnstyle from the FAQ, use the one below that has been modified to color only the current cell and has a Edit override to avoid the cell being editable.
Public Class DataGridColoredTextBoxColumn
    Inherits DataGridTextBoxColumn

    Private column As Integer ' column where this columnstyle is located...
    Public Sub New()
        column = -2
    End Sub

    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)

            Dim grid As DataGrid = Me.DataGridTableStyle.DataGrid

            'first time set the column properly
            If column = -2 Then
                Dim i As Integer
                i = Me.DataGridTableStyle.GridColumnStyles.IndexOf(Me)
                If i > -1 Then
                    column = i
                End If
            End If

            If grid.CurrentRowIndex = rowNum And grid.CurrentCell.ColumnNumber = column Then
                backBrush = New LinearGradientBrush(bounds, Color.FromArgb(255, 200, 200), Color.FromArgb(128, 20, 20), LinearGradientMode.BackwardDiagonal)
                foreBrush = New SolidBrush(Color.White)
            End If
        Catch ex As Exception
            ' empty catch 
            ' make sure the base class gets called to do the drawing with
            ' the possibly changed brushes
            MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
        End Try

    End Sub

    Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)

    ' do nothing... dont call baseclass
    End Sub
End Class

RU rufus July 23, 2002 12:05 AM

Clay Burch, Thanks. I think I got the idea from the code you provided.

Live Chat Icon For mobile
Up arrow icon