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
close icon

Color of current cell

Hello!

I tried to find the answer in the forum, but I could not obtain what I wanted.

I have a virtual grid that allows any selection. I want to set the background color for the selected cells.
I have implemented the PrepareViewStyleInfo event handler:
private void grid_PrepareViewStyleInfo( object sender, GridPrepareViewStyleInfoEventArgs e)
{
if (grid.Selections.Ranges.AnyRangeContains(GridRangeInfo.Cell( e.RowIndex, e.ColIndex)))
e.Style.BackColor = Color.Blue;
}
and it works fine except for the current cell (if I select only one cell or, using multiple selections, for the cell that remains the "current cell")
When selecting multiple cell using the CTRL key, I see the last selected cell becoming blue, than turning back to the default row color (I have alternate row colors).
It seems that this behavior exists because of the RichTextBox used for the current cell (my grid is read-only, so this behavior is not necessary).

What can I do?

Thanks in advance,
Lucia

6 Replies

RA Rajagopal Syncfusion Team August 15, 2007 01:26 AM UTC

Hi Lucia,

Actually when the currentcell is not in the editing state, it generally is part of the selections. It is just that the current cell is not included when the selection rectangle is drawn. This is by design. If you want to see it colored like other selected cells, then you can handle the CellDrawn event and color it there. Please try the code below.

void grid_CellDrawn(object sender, GridDrawCellEventArgs e)
{
GridCurrentCell cc = this.grid.CurrentCell;

if (e.ColIndex == cc.ColIndex && e.RowIndex == cc.RowIndex)
{
SolidBrush br = new SolidBrush(Color.Blue);
e.Graphics.FillRectangle(br, e.Bounds);
br.Dispose();
}
}

Here is a sample showing this
http://websamples.syncfusion.com/samples/Grid.Windows/F67332/main.htm

Let me know if you have any other questions.

Have a nice time.
Regards,
Rajagopal


LD Lucia Diaconu August 15, 2007 01:53 PM UTC

Hi Rajagpal!

I tried your solution and it is not exactly what I need:
1) Selecting with mouse-click:
1.a. at button down, the blue filled rectangle appears (see image SelectedCell_MouseDown)
1.b. at button up, the text appears and the blue color can be seen as a small left and even smaller right margin (see image SelectedCell_MouseUp)
2) selecting with the keyboard's arrow keys it is worse, because the text of the selected cell is not visible anymore (see image SelectedCell_Keyboard)

Something must be incorrect in my cell settings, but I don't know what (I hope you know :-) )

Thanks,
Lucia

I can't post the message, I try without the attachment first...


LD Lucia Diaconu August 16, 2007 02:11 PM UTC

Yesterday I could not attach the zip with the images. Maybe today it will be possible?

Syncfusion_SelectedCellBackColor.zip


RA Rajagopal Syncfusion Team August 17, 2007 02:48 AM UTC

Hi Lucia,

Please refer the sample below that handles the PrepareViewStyleInfo event to color the currentcell when it is a part of selection. This seems to work as expected.

Here is a sample
http://www.syncfusion.com/Support/user/uploads/GC_SelectionColrs1_512fa30e.zip

Let me know if this helps.

Regards,
Rajagopal


LD Lucia Diaconu August 20, 2007 01:13 PM UTC

Hi!
Yes, this did the job.
But it was very strange at first because I wanted to optimize the code and I have changed the initial sequence:
GridControl grid = sender as GridControl;
GridCurrentCell cc = grid.CurrentCell;
if (grid.Selections.Ranges.AnyRangeContains(GridRangeInfo.Cell(e.RowIndex, e.ColIndex)))
{
e.Style.BackColor = Color.DarkBlue;
}
if (e.RowIndex == cc.RowIndex && e.ColIndex == cc.ColIndex)
{
e.Style.BackColor = Color.DarkBlue;
}

into the sequence:
GridControl grid = sender as GridControl;
GridCurrentCell cc = grid.CurrentCell;
if (e.RowIndex == cc.RowIndex && e.ColIndex == cc.ColIndex)
{
e.Style.BackColor = Color.DarkBlue;
}
else
if (grid.Selections.Ranges.AnyRangeContains(GridRangeInfo.Cell(e.RowIndex, e.ColIndex)))
{
e.Style.BackColor = Color.DarkBlue;
}

and it had a very strange behavior:
1. when selecting several cell (e.g. CTRL + mouse or keypad selection), the previously selected cell's background became yellow. After that, a simple new selection left the old selected cell appear blue.
This is kind of strange for me because it seems I don't understand well the behavior of the grid :-((

Anyway, in the "good" order, it was ok.

Thank you!
Lucia



AD Administrator Syncfusion Team December 31, 2007 10:01 AM UTC

Hi Lucia,

Sorry for the delay in getting back to you.

Strange behavior - The background of the selected cell rectangle becomes yellow when the BackColor was set to DarkBlue.

This behavior was common with .NET DataGridView control. The color of the cursor is different from the BackColor we set especially for DarkCyan, DarkBlue, etc., when the cell is in edit mode.

Please refer the sample that compares the behavior of GridControl with DataGridView control and let me know if you have any further questions.

http://websamples.syncfusion.com/samples/Grid.Windows/F67332/Selection/main.htm

Thanks for your patience.

Regards,
Jaya



Loader.
Live Chat Icon For mobile
Up arrow icon