C#Winforms DataGridView粗体字体样式

时间:2016-08-10 14:18:46

标签: c# winforms datagridview

为什么行中没有文字? 如何使行以粗体显示?

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; }
    }
}

2 个答案:

答案 0 :(得分:4)

您缺少列的DataPropertyName属性。

  

获取或设置DataGridViewColumn绑定到的数据源属性或数据库列的名称。

将其更改为:

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 } 
});

那应该用粗体字体添加行。

答案 1 :(得分: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;
}