Monday 17 October 2016

How to get the Style.BackColor of a datagridview cell



Using VS2010 VB.NET, I have a datagridview (dgv) where I set the background color of cells according to a certain value. This particular cell does not store any information and therefore I can not use the data in that cell to determine the background color.



My current attempts are a complete failure and MSDN is only particular to "setting" and not "getting" the background color of a cell.



Code I've tried"



if dgvNotes.Rows(clickedCell.RowIndex).Cells(1).Style.BackColor.ToString = "Red" then


if dgvNotes.Rows(clickedCell.RowIndex).Cells(1).Style.BackColor = Color.Red then


I am not able to find much information on the topic as most of the posts on various coding sites focusses on "setting" the background color.



Is there any way that I can determine cell background color after the dgv was populated ?
Thanks


Answer



write in cell click event




// using vb.net
System.Drawing.Color c = dgvNotes.Rows(e.RowIndex).Cells(1).Style.BackColor;

//using c#
System.Drawing.Color c = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor;

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...