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