468,308 Members | 1,212 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,308 developers. It's quick & easy.

Datagrideview font color of single cell

Hi
is there any way to change the font color of single cells for each row separately if I use check boxes checked or not, for each one separately and save it in Datagridview visual basic Datagridview connect to access database?
Mar 28 '20 #1
2 1807
SioSio
240 128KB
To change the font color of a specific cell by turning on/off the Checkbox, follow the steps below.
Expand|Select|Wrap|Line Numbers
  1.     Sub CheckBox1CheckedChanged(sender As Object, e As EventArgs)
  2.         If checkBox1.Checked = True Then
  3.             DataGridView1(1, 1).Style.ForeColor = Color.Blue
  4.         Else
  5.             DataGridView1(1, 1).Style.ForeColor = Color.Red
  6.         End If
  7.     End Sub
Mar 30 '20 #2
You can do this:

dataGridView1.SelectedCells[0].Style
= new DataGridViewCellStyle { ForeColor = Color.Yellow};
You can also set whatever style settings (font, for example) in that cell style constructor.

And if you want to set a particular column text color you could do:

dataGridView1.Columns[colName].DefaultCellStyle.ForeColor = Color.Yellow;
dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Blue;
updated

So if you want to color based on having a value in the cell, something like this will do the trick:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null && !string.IsNullOrWhiteSpace(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()))
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = new DataGridViewCellStyle { ForeColor = Color.Orange, BackColor = Color.Blue };
}
else
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = dataGridView1.DefaultCellStyle;
}
}
Apr 4 '20 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by PCK | last post: by
5 posts views Thread by jack | last post: by
1 post views Thread by hermitking | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.