By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,268 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,268 IT Pros & Developers. It's quick & easy.

Datagrideview font color of single cell

P: 1
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
Share this Question
Share on Google+
2 Replies


100+
P: 150
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

P: 37
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.