This example shows how to draw a thick border around a grid cell under the mouse pointer:
The example handles the grid's PaintEx event to draw a border. The PaintEx
event allows you to use DirectX-compatible APIs (e.Cache
):
C#private void GridControl_PaintEx(object sender, DevExpress.XtraGrid.PaintExEventArgs e) {
DrawHotTrackedCell(e.Cache);
}
private void DrawHotTrackedCell(GraphicsCache cache) {
Rectangle bounds = GetCellBounds(HotTrackedCell);
cache.DrawRectangle(new Pen(Brushes.Black, _BorderWidth), bounds);
}
Files to Review
- Form1.cs (VB: Form1.vb)
- Program.cs (VB: Program.vb)
Documentation
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.Utils.Drawing;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private DataTable CreateTable(int RowCount)
{
DataTable tbl = new DataTable();
tbl.Columns.Add("Name", typeof(string));
tbl.Columns.Add("ID", typeof(int));
tbl.Columns.Add("Number", typeof(int));
tbl.Columns.Add("Date", typeof(DateTime));
for (int i = 0; i < RowCount; i++)
tbl.Rows.Add(new object[] { String.Format("Name{0}", i), i, 3 - i, DateTime.Now.AddDays(i) });
return tbl;
}
public Form1()
{
InitializeComponent();
gridControl1.DataSource = CreateTable(20);
new HotTrackCellHelper(gridView1);
}
}
public class HotTrackCellHelper
{
public HotTrackCellHelper(GridView view)
{
_View = view;
view.GridControl.PaintEx += GridControl_PaintEx;
view.MouseMove += new MouseEventHandler(view_MouseMove);
}
private void GridControl_PaintEx(object sender, DevExpress.XtraGrid.PaintExEventArgs e)
{
DrawHotTrackedCell(e.Cache);
}
private int _BorderWidth = 4;
private GridCell _HotTrackedCell;
private readonly GridView _View;
public GridCell HotTrackedCell
{
get { return _HotTrackedCell; }
set
{
RefreshCell(_HotTrackedCell);
_HotTrackedCell = value;
}
}
public Rectangle GetCellBounds(GridCell cell)
{
if (cell == null)
return Rectangle.Empty;
GridViewInfo info = _View.GetViewInfo() as GridViewInfo;
GridCellInfo cellInfo = info.GetGridCellInfo(cell.RowHandle, cell.Column);
return cellInfo.Bounds;
}
private void UpdateHotTrackedCell(Point location)
{
GridHitInfo hi = _View.CalcHitInfo(location);
if (hi.HitTest == GridHitTest.Row || hi.HitTest == GridHitTest.RowEdge)
return;
if (hi.InRowCell)
{
if (_View.IsRowVisible(hi.RowHandle) == RowVisibleState.Visible)
{
HotTrackedCell = new GridCell(hi.RowHandle, hi.Column);
return;
}
}
HotTrackedCell = null;
}
void view_MouseMove(object sender, MouseEventArgs e)
{
UpdateHotTrackedCell(e.Location);
}
private void RefreshCell(GridCell cell)
{
if (cell == null)
return;
Rectangle rect = GetCellBounds(cell);
rect.Inflate(_BorderWidth, _BorderWidth);
_View.InvalidateRect(rect);
}
private void DrawHotTrackedCell(GraphicsCache cache)
{
Rectangle bounds = GetCellBounds(HotTrackedCell);
cache.DrawRectangle(new Pen(Brushes.Black, _BorderWidth), bounds);
}
}
}
C#using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}