A191919 A191919 - 3 months ago 32
C# Question

C# Winforms DataGridView bold font style in rows

Why there is no text in rows?
How to make text in rows bold?

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.Columns.Add(new DataGridViewColumn { HeaderText = "Test", CellTemplate = new DataGridViewTextBoxCell() { }, DefaultCellStyle = new DataGridViewCellStyle { Font = new Font("Tahoma", 9.75F, FontStyle.Bold) } });
this.dataGridView1.DataSource = new List<Employee>
{
new Employee {Name="Test"},
new Employee {Name="Test"},
new Employee {Name="Test"},
new Employee {Name="Test"},
new Employee {Name="Test"},
new Employee {Name="Test"},
new Employee {Name="Test"}
};
}
}
public class Employee
{
public string Name { get; set; }

public int Number { get; set; }
}
}

Answer

You are missing the DataPropertyName property for your column.

Gets or sets the name of the data source property or database column to which the DataGridViewColumn is bound.

Change it to this:

this.dataGridView1.Columns.Add(new DataGridViewColumn { HeaderText = "Test", DataPropertyName = "Name", CellTemplate = new DataGridViewTextBoxCell() { }, DefaultCellStyle = new DataGridViewCellStyle { Font = new Font("Tahoma", 9.75F, FontStyle.Bold), ForeColor = Color.Black } });

That should add the rows with bolded font.

Comments