Windows Develop Bookmark and Share   
 index > Windows Forms General > Datagridview cell backcolor
 

Datagridview cell backcolor

I can programmatically set the backcolor of a cell depending on its value but how can I then set the backcolor of an adjacent cell in the same row to the same color?

TIA

Richard Creer  Wednesday, September 30, 2009 5:28 PM
Can you add to your post the code you are using to set the backcolor of the original cell?
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Wednesday, September 30, 2009 5:40 PM

Hi Richard,

From my experience, we can only set the color of the cells in a row or a column. This is the code snippet:
    //Set the backcolor of the cells in the first row.

    dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Red;

    //Set the backcolor of the cells in the first column.

dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Red;

Based on my understanding, you did set the backcolor of one column and want to set the backcolor of one cell to another cell in the same row. You can follow the code snippet below:
    //Set the backcolor of column 2 to the backcolor of column 1.

dataGridView1.Columns[2].DefaultCellStyle.BackColor =   dataGridView1.Columns[1].DefaultCellStyle.BackColor;

We can also handle the CellPainting event to redraw the background of one cell to change the backcolor of only one cell. Did you set the color of one cell in this way?

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, October 05, 2009 7:10 AM

Hi,

We are changing the issue type to “General Discussion” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question” by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, October 08, 2009 4:48 AM

You can use google to search for other answers

Custom Search

More Threads

• Dynamic Tabcontrol from database
• data grid binding
• How to hide the textbox portion of a combo box
• GDI handles leak
• insert values in to database using ado.net -sql server 2005 & visual basic 2005
• OpenFileDialog fails to open the specified path
• Prevent the ReDraw of a Button Control
• yet another datagrid question
• How to upgrade settings if app uses different strong name every revision.
• mouse click +image+C#