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

How can I highlight the current cell in DataGrid?

Thread ID:

Created:

Updated:

Platform:

Replies:

627 Jul 17,2002 07:21 PM Jul 23,2002 12:05 AM Windows Forms 4
loading
Tags: FAQ
rufus
Asked On July 17, 2002 07:21 PM

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

rufus
Replied On 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.

Clay Burch [Syncfusion]
Replied On 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)

        Try
            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 
        Finally
            ' 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

rufus
Replied On July 23, 2002 12:05 AM

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

CONFIRMATION

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.

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.

;