Setting a style for a selected row in the DataGridView

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

Hi

Can anyone tell me if there is a property for setting the default cell
style for a cell thats selected. I can see properties for the default
cell styling and alternate row styling but not for selected styles.

Am I missing something here? Do I have to write some custom formatting code?

Many thanks

Simon
 
The selection settings is taken from the system setting (Advanced
Appearance tab of the Display settings om the Control Panel). One way
you can override this default behavior is to handle the CellPainting
event.

void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{

if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
e.Paint(e.ClipBounds, DataGridViewPaintParts.Background
| DataGridViewPaintParts.Border
| DataGridViewPaintParts.ContentBackground
| DataGridViewPaintParts.ContentForeground);
if
(this.dataGridView1.SelectedCells.Contains(this.dataGridView1[e.ColumnIndex,
e.RowIndex]))
{
using (Brush b = new SolidBrush(Color.FromArgb(60,
Color.Red)))
{
e.Graphics.FillRectangle(b, e.CellBounds);
}
}
e.Handled = true;
}
}
==================
Clay Burch
Syncfusion, Inc.
 
Woah - holly cow - I was kinda hoping there was just a property that I
could use to set tyle stuff!

Or maybe using some "row selected" or "on selection" changed style event.

All that painting stuff seems hard! Is there something simpler?

Many thanks for your help

Simon
 
Simon Harvey schreef:
Hi

Can anyone tell me if there is a property for setting the default cell
style for a cell thats selected. I can see properties for the default
cell styling and alternate row styling but not for selected styles.

Am I missing something here? Do I have to write some custom formatting
code?

There are DefaultCellStyle.SelectionForeColor and ..SelectionBackColor
 
Back
Top