In this example, we’ll learn, how to change the text style of a DataGridView in C#.
The following code will change the font of the text in the first row of the DataGridView to be bold and red. You can adjust the style and the row index to suit your needs.
1 2 3 4 5 6 7 8 9 10 |
private void Form1_Load(object sender, EventArgs e) { DataGridViewCellStyle style = new DataGridViewCellStyle(); style.Font = new Font(dataGridView1.Font, FontStyle.Bold); style.ForeColor = Color.Red; dataGridView1.Rows[0].DefaultCellStyle = style; } |
You can also change the style for all cells in the DataGridView by setting the DefaultCellStyle
property of the DataGridView itself, rather than just a specific row:
1 2 3 4 |
dataGridView1.DefaultCellStyle.Font = new Font(dataGridView1.Font, FontStyle.Bold); dataGridView1.DefaultCellStyle.ForeColor = Color.Red; |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
private void Form1_Load(object sender, EventArgs e) { DataTable table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Age", typeof(int)); table.Rows.Add("John", 30); table.Rows.Add("Jane", 25); table.Rows.Add("Colin", 22); table.Rows.Add("Joshua", 25); dataGridView1.DataSource = table; dataGridView1.DefaultCellStyle.Font = new Font(dataGridView1.Font, FontStyle.Bold); dataGridView1.DefaultCellStyle.ForeColor = Color.Red; DataGridViewCellStyle style = new DataGridViewCellStyle(); style.BackColor = Color.LightBlue; style.ForeColor = Color.Black; style.Font = new Font(dataGridView1.Font, FontStyle.Bold); dataGridView1.DefaultCellStyle = style; } |
Output: