Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing datagridview cell color dynamically

I have a dataGridView object that is populated with data. I want to click a button and have it change the color of the background of the cell. This is what I currently have

foreach(DataGridViewRow row in dataGridView1.Rows) {     foreach(DataGridViewColumn col in dataGridView1.Columns)     {             //row.Cells[col.Index].Style.BackColor = Color.Green; //doesn't work             //col.Cells[row.Index].Style.BackColor = Color.Green; //doesn't work         dataGridView1[col.Index, row.Index].Style.BackColor = Color.Green; //doesn't work     } }  

ALL of these three cause the table to be redrawn over itself in an overlapping manner and trying to re-size the tables becomes a mess. when clicking on a cell, the value remains highlighted and the backcolor doesn't change.

Q: How can I change the backcolor of an individual cell after the table exists?

like image 314
fifamaniac04 Avatar asked Jul 18 '13 15:07

fifamaniac04


1 Answers

This works for me

dataGridView1.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Red; 
like image 70
Ehsan Avatar answered Oct 14 '22 12:10

Ehsan