Windows Develop Bookmark and Share   
 index > Windows Forms General > DatagrifView AlternatingRowsDefaultCellStyle overides the DefaultCellStyle.BackColor
 

DatagrifView AlternatingRowsDefaultCellStyle overides the DefaultCellStyle.BackColor

Hi,

I want to change a columns background color: dgvtxbQty.DefaultCellStyle.BackColor = System.Drawing.Color.White;

But on the Grid I have a AlternatingRowsDefaultCellStyle which set every other row format.

The problem is it overides the dgvtxbQty.DefaultCellStyle.BackColor & the backcolor only works for the rows not being set by the AlternatingRowsDefaultCellStyle

Is there a way around this?

Regards
  • Moved byTaylorMichaelLMVPTuesday, September 29, 2009 1:56 PMWinForms related (From:Visual C# General)
  •  
Ismailc  Tuesday, September 29, 2009 1:16 PM
Here's my quick and dirty way:

private void dataGridView1_CellPainting ( object sender, DataGridViewCellPaintingEventArgs e )
{
    if (e.ColumnIndex == 0)  //Desired column index goes here
    {
        DataGridView dgv = sender as DataGridView;

        e.CellStyle.BackColor = dgv.Columns[e.ColumnIndex].DefaultCellStyle.BackColor;
    };
}
I haven't made it production ready nor have I confirmed whether it behaves well when the grid has a lot of elements but it works for the situation you gave.

Michael Taylor - 9/29/09
http://p3net.mvps.org
TaylorMichaelL  Tuesday, September 29, 2009 1:56 PM
My original code sample should work with alternating rows. That's how I tested it. Did you hook up the CellPainting event to the handler? Just to be sure we are talking about WinForms right?

To change the color of a column with selection you're going to follow the same pattern. But in this case the style property is SelectionBackColor. Here's the full code to change column 0 to use green for the background, red for alternating rows and yellow for the selected row.

private void dataGridView1_CellPainting ( object sender, DataGridViewCellPaintingEventArgs e )
{
    DataGridView dgv = sender as DataGridView;

    if (e.ColumnIndex == 0)
    {
        e.CellStyle.SelectionBackColor = Color.Yellow;

        if (e.RowIndex % 2 == 0)
            e.CellStyle.BackColor = Color.Red;
        else
            e.CellStyle.BackColor = Color.Green;
    };
}

//In InitializeComponent
this.dataGridView1.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.dataGridView1_CellPainting);
Note that there are more events available off of DGV that can control other aspects of painting but this is the one you'd use for controlling individual cells. Note that column headers get their own events but the actual columns don't have paint methods since the cell is responsible for painting them.

If this still doesn't work for you then please post the handler code and the DGV settings code so we can take a closer look.

Michael Taylor - 9/30/09
http://p3net.mvps.org
TaylorMichaelL  Wednesday, September 30, 2009 1:38 PM
Here's my quick and dirty way:

private void dataGridView1_CellPainting ( object sender, DataGridViewCellPaintingEventArgs e )
{
    if (e.ColumnIndex == 0)  //Desired column index goes here
    {
        DataGridView dgv = sender as DataGridView;

        e.CellStyle.BackColor = dgv.Columns[e.ColumnIndex].DefaultCellStyle.BackColor;
    };
}
I haven't made it production ready nor have I confirmed whether it behaves well when the grid has a lot of elements but it works for the situation you gave.

Michael Taylor - 9/29/09
http://p3net.mvps.org
TaylorMichaelL  Tuesday, September 29, 2009 1:56 PM
H, Thank You

I have tried:DataGridView dgv = sender as DataGridView;
e.CellStyle.BackColor = dgvDetail.Columns[e.ColumnIndex].DefaultCellStyle.BackColor;

even : DataGridView dgv = sender as DataGridView;
e.CellStyle.BackColor = dgv.Columns[e.ColumnIndex].DefaultCellStyle.BackColor;

but unfortunately the alternatingrowdefaultcellstyle still overides it, but I decided to removes this event and just set the background color.
Ismailc  Wednesday, September 30, 2009 9:52 AM
Sorry still struggling.

I removed the alternatingrowdefaultcellstyle but the RowSelectionColor is now overiding the cell background color.


Any ideas?
Ismailc  Wednesday, September 30, 2009 10:18 AM
My original code sample should work with alternating rows. That's how I tested it. Did you hook up the CellPainting event to the handler? Just to be sure we are talking about WinForms right?

To change the color of a column with selection you're going to follow the same pattern. But in this case the style property is SelectionBackColor. Here's the full code to change column 0 to use green for the background, red for alternating rows and yellow for the selected row.

private void dataGridView1_CellPainting ( object sender, DataGridViewCellPaintingEventArgs e )
{
    DataGridView dgv = sender as DataGridView;

    if (e.ColumnIndex == 0)
    {
        e.CellStyle.SelectionBackColor = Color.Yellow;

        if (e.RowIndex % 2 == 0)
            e.CellStyle.BackColor = Color.Red;
        else
            e.CellStyle.BackColor = Color.Green;
    };
}

//In InitializeComponent
this.dataGridView1.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.dataGridView1_CellPainting);
Note that there are more events available off of DGV that can control other aspects of painting but this is the one you'd use for controlling individual cells. Note that column headers get their own events but the actual columns don't have paint methods since the cell is responsible for painting them.

If this still doesn't work for you then please post the handler code and the DGV settings code so we can take a closer look.

Michael Taylor - 9/30/09
http://p3net.mvps.org
TaylorMichaelL  Wednesday, September 30, 2009 1:38 PM

You can use google to search for other answers

Custom Search

More Threads

• gui threads with controls bound to the same data source
• XP themes are not being applied to my Form controls.
• Docking one form to another
• Unwanted Selectindex event fired up!!
• Executing a code or event when the program is opened when clicking a file...?
• .net 2.0 scheduled task
• GDI+ SourceCopy
• Tool strip control is not aligned to left
• WMPOCX.OCX help about URL property
• How to make a usercontrol to act as an container?