3

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; }
    }
}
A191919
  • 3,422
  • 7
  • 49
  • 93

2 Answers2

4

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.

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
0
public Form1()
{
    InitializeComponent();
    BindingList<Employee> list = new BindingList<Employee>();
    list.Add(new Employee() { Name = "Test1" });
    list.Add(new Employee() { Name = "Test2" });
    list.Add(new Employee() { Name = "Test3" });
    list.Add(new Employee() { Name = "Test4" });
    list.Add(new Employee() { Name = "Test5" });

    dataGridView1.DataSource = list;
}
kassi
  • 358
  • 1
  • 3
  • 10