【问题标题】:Centering Column and Row Text Content in DataGridView在 DataGridView 中居中列和行文本内容
【发布时间】:2021-03-11 01:49:55
【问题描述】:

我想在 DataGridView 上将列和行的内容居中。

有没有办法通过指定列名来做到这一点,例如:

dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; 

我希望将其应用于我的 DataGridView 的所有列(完全以编程方式编码)。

我该怎么做?

【问题讨论】:

    标签: c# datagridview alignment


    【解决方案1】:

    试试这个。

    dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
    

    【讨论】:

    • 这会产生与 OP 代码相同的结果吗?我认为 MiddleRight 的对齐方式会略有不同,但不确定。
    • 我已经更正了。我已经发布了网络解决方案,但我意识到问题是关于 Windows 表单数据网格的。
    • 它不适用于 Telerik 中的 radGridView。我在为 Telerik 做什么? @Telerik
    【解决方案2】:

    如果您的 DataGridView 中有 DataGridViewTextBoxColumn 列,它可能看起来像

    var col1 = new DataGridViewTextBoxColumn()
    {
       HeaderText = "Month",
       Width = 40,
       DefaultCellStyle = new DataGridViewCellStyle()
       {
          Alignment = DataGridViewContentAlignment.MiddleCenter,
          ForeColor = System.Drawing.Color.Black
       }
    };
    
    dataGridView.Columns.Add(col1);
    

    【讨论】:

      猜你喜欢
      • 2016-10-07
      • 2013-08-11
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      相关资源
      最近更新 更多