【问题标题】:C# Winforms DataGridView bold font style in rowsC# Winforms DataGridView 行中的粗体字体样式
【发布时间】:2016-08-10 23:02:20
【问题描述】:

为什么行中没有文本? 如何使行中的文本加粗?

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

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    您的列缺少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 } 
    });
    

    这应该添加带有粗体字体的行。

    【讨论】:

      【解决方案2】:
      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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-23
        • 2011-08-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多