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
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How can I highlight the current cell in DataGrid?

Thread ID:





627 Jul 17,2002 11:21 PM UTC Jul 23,2002 04:05 AM UTC WinForms 4
Tags: FAQ
Asked On July 17, 2002 11:21 PM UTC

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

Clay Burch [Syncfusion]
Replied On July 18, 2002 10:10 AM UTC

> 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?

Replied On July 18, 2002 06:48 PM UTC

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.

Clay Burch [Syncfusion]
Replied On July 21, 2002 07:25 AM UTC

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

Replied On July 23, 2002 04:05 AM UTC

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


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon