KB Article A1018
Visible to All Users

How to draw a thick border for a grid cell

Description:
Can I draw a box around the selected cell? I need to implement an Excel-type cell focus rectangle.

Answer:
This article explains three different approaches to accomplishing this. It's up to you to decide which one to use.

  1. Use the custom drawing feature of the XtraGrid, namely, the GridView.CustomDrawCell event. The specifics of this approach is that you have to fully paint the cell's interior and then draw a thick border over it. You can draw the editors' contents by calling the e.DefaultDraw method (or the grid's internal method in older versions of our components). This approach is shown in the How to draw thick cell borders by handling the CustomDrawCell event example.
    A cell's border painted via the CustomDrawCell event handler is displayed only when the cell is not being edited. When the in-place editor is opened, it paints itself and does not support custom drawing. However, you can draw borders for the different cells of your grid, not just for the focused cell, and use different colors for them. Thus, you can use this solution for highlighting cells as an alternative to cell styles (see the Assigning Styles to Individual Cells help topic).

  2. Use the PaintEx (starting with version 17.2 of our components) or Paint event (in older versions) as shown in the How to draw thick cell borders by handling the Paint event example. The basic idea of this approach is to handle the Paint/PaintEx event, obtain GridViewInfo and GridCellInfo  objects to calculate a cell's bounds and paint a border around the focused cell. All this work is done by the special HotTrackCellHelper  class implemented in the scope of the How to draw thick cell borders by handling the Paint event example.

See Also:
Custom Painting Basics
How to draw a rectangle around selected cells and implement a copy/paste functionality
How to merge cells horizontally in GridView
How to copy a cell's value to other cells by dragging its right bottom edge

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.