Home All Groups Group Topic Archive Search About

Setting a style for a selected row in the DataGridView

Author
12 Feb 2007 10:13 AM
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
Author
12 Feb 2007 12:15 PM
ClayB
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.
Are all your drivers up to date? click for free checkup

Author
12 Feb 2007 7:42 PM
Simon Harvey
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
Author
12 Feb 2007 7:42 PM
Tim Van Wassenhove
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


--
Tim Van Wassenhove <url:http://www.timvw.be/>
Author
13 Feb 2007 3:25 PM
Simon Harvey
YOU ARE A STALLION!

Show quoteHide quote
:-)

Bookmark and Share