更改DataGridView的标题单元格“文本对齐方式和字体大小 [英] Changing DataGridView Header Cells' Text Alignment And The Font Size

查看:3156
本文介绍了更改DataGridView的标题单元格“文本对齐方式和字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变文本的对齐方式和DataGridView的字体大小。所有列在运行时编程创建。下面的代码..

I'm trying to change the text alignment and the font size of a DataGridView. All the Columns are created programatically at runtime. Here's the code..

private void LoadData()
{
    dgvBreakDowns.ColumnCount = 5;
    dgvBreakDowns.Columns[0].Name = "Breakdown No";
    dgvBreakDowns.Columns[1].Name = "Breakdown Type";
    dgvBreakDowns.Columns[2].Name = "Machine Type";
    dgvBreakDowns.Columns[3].Name = "Date";
    dgvBreakDowns.Columns[4].Name = "Completed";

    dgvBreakDowns.Columns[4].Visible = false;

    foreach (DataGridViewHeaderCell header in dgvBreakDowns.Rows)
    {
        header.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        header.Style.Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel);
    }
}

LoadData()方法被调用在窗体的构造函数。创建列,但他们的头'的变化并不适用。
我认为它在我的循环的foreach(DataGridViewHeaderCell头球dgvBreakDowns.Rows)?一大败笔,因为我不知道。我试图将其更改为 dgvBreakDowns.Columns ,我得到一个InvalidCastException。如何选择标题单元格应用这些变化?

This LoadData() method is called in the Form's constructor. The Columns are created but their Headers' changes don't apply. I think its because of a flaw in my loop foreach (DataGridViewHeaderCell header in dgvBreakDowns.Rows)? I'm not sure. I tried changing it to dgvBreakDowns.Columns and I get an InvalidCastException. How can I select the Header Cells to apply those changes?

我还有一个小问题。当我运行它看起来像这样的程序。

I have another minor issue. When I run the program it looks like this.

注意,第一个单元格是默认选择,因此它出现蓝色。确保它不会影响任何东西,但它只是看起来有点丑陋和邋遢。这可以从选择喜欢的细胞阻止它?

Notice the first Cell is selected by default therefore it appears Blue. Sure it doesn't affect anything but it just looks somewhat ugly and untidy. It is possible to stop it from selecting the Cell like that?

推荐答案

试试这个(注意:我使用的列这里,而不是行):

Try this (note I'm using Columns here and not Rows):

foreach(DataGridViewColumn col in dgvBreakDowns.Columns)
{
    col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    col.HeaderCell.Style.Font = new Font("Arial", 12F, FontStyle.Bold, GraphicsUnit.Pixel);
}



至于取消选择单元格,尝试的 dgvBreakDowns.ClearSelection()

这篇关于更改DataGridView的标题单元格“文本对齐方式和字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆