【问题标题】:Change font size in a column in DataGridView更改 DataGridView 中列中的字体大小
【发布时间】:2013-03-15 13:29:37
【问题描述】:

我在 DataGridView(WinForm 应用程序)中有一个列,需要更改字体大小和样式。从这里的文章:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.font.aspx,我认为下面的代码会得到我想要的结果(我正在通过首先更改样式进行测试):

this.dataGridViewMain.Columns[3].DefaultCellStyle.Font = new Font(dataGridViewMain.DefaultCellStyle.Font, FontStyle.Italic);

但是代码并没有改变任何东西。我还尝试在RowPostPaint 事件处理程序上添加代码,但仍然无法正常工作。我知道程序使用的字体是在DataGridView.RowsDefaultCellStyle 属性上设置的,但我认为将代码放在RowPostPaint 事件中会覆盖它。以下是来自RowPostPaint 事件的代码:

void dataGridViewMain_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    this.dataGridViewMain.Columns[3].DefaultCellStyle.BackColor = Color.Gray;
    foreach (DataGridViewRow row in this.dataGridViewMain.Rows)
    {
        int daysInShop = Convert.ToInt32(row.Cells["Days in the shop"].Value);
        if (daysInShop > 4)
        {
            row.DefaultCellStyle.BackColor = Color.Red;
            row.DefaultCellStyle.ForeColor = Color.White;
        }
        else if (daysInShop > 2)
        {
            row.DefaultCellStyle.BackColor = Color.Yellow;
        }
        else
        {
            row.DefaultCellStyle.BackColor = Color.YellowGreen;
        }
        row.Height = 35;
    }

    this.dataGridViewMain.CurrentCell = null; // no row is selected when DGV is displayed
}

感谢任何帮助。谢谢。

【问题讨论】:

  • 我测试了您的第一个语句以将列更改为斜体,它工作得非常好。请注意,我在已经填充了 datagridview 之后添加了这一点。以前没有。
  • 有趣。我已将 DataGridView 上的 RowsDefaultCellStyle 属性设置为 Verdana、14.25pt 和 Bold。这种风格就是出现的。我的斜体列代码不会覆盖该属性。此外,我用 DataTable 填写了 DataGridView,而不是在 DataGridView 中创建自己的列(不确定这是否与问题有关)
  • 我也用数据表填充了我的。
  • 这是一个相当古老的线程,但偶尔仍然有用。我的小贡献是不需要循环,你可以使用 e->RowIndex 来更加专注,因为你会收到每一行的调用。
  • 另外,使用 PrePaint 而不是 PostPaint

标签: c# winforms


【解决方案1】:

好的,这就是我发现的。在InitializeComponent() 下有这一行:

dataGridViewCellStyle3.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

当我注释掉该行时,将RowPostPaint 中的列斜体化的代码可以正常工作。然后我在RowPostPaint 中添加了下面的代码,以便其他列字体为粗体且尺寸更小。我仍然不太清楚为什么DataGridView.Columns[colNumber].DefaultCellStyle.Font 不会覆盖dataGridViewCellStyle3

 int colCount = dataGridViewMain.ColumnCount;

 for (int i = 0; i < colCount; i++)
 {
     if(i != 3)
         this.dataGridViewMain.Columns[i].DefaultCellStyle.Font = new System.Drawing.Font("Verdana", 14F, FontStyle.Bold);
     else
         this.dataGridViewMain.Columns[3].DefaultCellStyle.Font = new System.Drawing.Font("Verdana", 25F, FontStyle.Bold);
 }

【讨论】:

    【解决方案2】:

    字体大小是只读的,因此您需要创建一个新字体并设置 yourDataGridView.Font = new Font(name,size,style) 这里有更多信息:http://msdn.microsoft.com/en-us/library/system.drawing.font.aspx

    【讨论】:

    • 是的,我之前尝试过,但对我来说,问题是样式甚至不起作用,所以我注释掉了新字体的代码
    • 我尝试在设置填充DataGridView后添加代码。代码:dataGridViewMain.DataSource = table;还尝试将代码放在 RowPostPaint 事件中。它们都不起作用。
    【解决方案3】:

    InitializeComponent() 之后将RowsDefaultCellStyle 设置为null

    我认为DataGridView 采用网格/列/行顺序的样式。因此,如果设置了行样式,它总是会覆盖任何列样式。

    在我看来,这是一个糟糕的设计——根本不需要默认的行样式!

    【讨论】:

      【解决方案4】:

      试试这个:

      foreach (DataGridViewRow dr in dataGridView.Rows)
      {
           if ( // your condition here )
           {
              dr.Cells[0].Style.Font = new Font( dataGridView.Font, FontStyle.Underline);
              dr.Cells[0].Style.ForeColor = Color.White;      
              dr.Cells[0].Style.BackColor = Color.Red;
           }
           else
           {     
            // It also may be a good idea to restore original settings
            // for the non selected rows just in case you re run this routine               
              dr.Cells[0].Style.BackColor = dataGridView.Columns[0].DefaultCellStyle.BackColor;
              dr.Cells[0].Style.ForeColor = dataGridView.Columns[0].DefaultCellStyle.ForeColor;
              dr.Cells[0].Style.Font = dataGridView.Columns[0].DefaultCellStyle.Font;
          }
      }
      

      【讨论】:

      • 这个问题已经回答了,您的帖子似乎没有添加任何新信息。
      猜你喜欢
      • 2014-02-18
      • 2015-04-08
      • 1970-01-01
      • 2019-07-10
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      相关资源
      最近更新 更多