DataGridView覆盖绘制以设置字体

时间:2012-11-17 02:12:26

标签: c# datagridview fonts styles

我试图以编程方式更改DataGridViews“Cell Header”字体样式 Col标头必须是UpperCase,我想分配一个新字体。 如果有人这样做之前我会感激你的指导。

更新

实际上,字体更改工作正常,headerText.ToUpper()我需要帮助

private void dataGridView1_Painting(object sender, DataGridViewCellPaintingEventArgs e)
{
            //Something like this.
           foreach(DataGridViewColumnCollection c in grd.Columns) {
               c.ColHeading.Text = c.ColHeading.Text.ToUpper(); 
           }

            //or
            //header row only
            if (e.RowIndex == -1)
            { 
                e.CellStyle.Font = new Font("Verdana", 11.0f);
                e.CellStyle.ForeColor = Color.Gray;
                e.Value = e.Value.ToUpper(); //fails as its a read only object
            }
}

1 个答案:

答案 0 :(得分:1)

不熟悉此控件,但浏览其他事件。可能不会及早做出这种改变,并且在“早期”活动中获得更好的运气。

行或列本身可能存在事件,您应该连接到这些事件来实现此目的。

根据评论IEnumerable的解决方案:

dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e) { e.Column.HeaderText = e.Column.HeaderText.ToUpper(); }