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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Hosting generic controls and drawing the focus rectangle around it

Thread ID:





51473 Nov 7,2006 02:14 AM UTC Nov 8,2006 04:01 PM UTC WinForms 4
Tags: GridControl
Administrator [Syncfusion]
Asked On November 7, 2006 02:14 AM UTC


It looks like the "focus" rectangle is drawn inside the cell; if you host a custom control, it occupies the whole cell so there is no way to see which control has focus. Of course, you can draw the black border in custom control, but we have many of them, and some of them don't support such behavior. Is is possible to simply draw the black gridlines around the active cell instead of drawing them inside the cell?

Thanks a lot

Administrator [Syncfusion]
Replied On November 7, 2006 08:42 AM UTC

Hi Andrew,

If you want to draw the focus rectangle in arbitrary cell types, it probably will be simpler to handle the CellDrawn event, and just draw the focusrectanlge there after the grid has finished doing its default drawing. Below is a code snippet

GridControl grid = sender as GridControl;
GridCurrentCell cc = grid.CurrentCell;
if( cc != null
&& cc.RowIndex == e.RowIndex
&& cc.ColIndex == e.ColIndex
&& e.Style.CellType == "MyCellType" )
e.Renderer.Draw(e.Graphics, e.Bounds, e.RowIndex, e.ColIndex, e.Style);
e.Cancel = true;

//Draw the focus rctangle Here...
Rectangle rect = e.Bounds;
rect.Inflate(- 5,-5);
rect.Size = new Size(rect.Width -5 ,rect.Height - 5);
e.Graphics.DrawRectangle( Pens.Black,rect);

Best Regards,

Administrator [Syncfusion]
Replied On November 7, 2006 05:02 PM UTC


Thanks for the quick answer.

Your example works perfectly for all cell renderers except those that host custom controls :)


Administrator [Syncfusion]
Replied On November 8, 2006 05:28 AM UTC

Hi Andrew,

You need to override the OnDraw method of your custom cell renderer. Here is a code snippet for drawing focused rectangle in Calender cell.

protected override void OnDraw(Graphics g, Rectangle clientRectangle, int rowIndex, int colIndex, GridStyleInfo style)
clientRectangle.Size = GridUtil.Max(clientRectangle.Size, CalendarCellModel.CalendarControlSize);
if (this.ShouldDrawFocused(rowIndex, colIndex))
style.Control = editCalendar;
g.DrawRectangle( new Pen(Brushes.Black,5f),clientRectangle);
clientRectangle.Size = new Size(clientRectangle.Width - 5,clientRectangle.Height - 5);
style.Control = drawCalendar;
CalendarCellModel.InitializeCalendar(drawCalendar, style);
base.OnDraw (g, clientRectangle, rowIndex, colIndex, style);

Here is a sample.

Best Regards,

Administrator [Syncfusion]
Replied On November 8, 2006 04:01 PM UTC


Thanks a lot for the example! I didn't know that it's possible to modify clientRectangle... simply inserting

clientRectangle.Inflate(-2, -2);

before base.OnDraw() did the trick for me.

Thanks again!


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

or the page will be automatically redirected to 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