如何在 C# 中自定义 Datagridview 标题

C# DataGridView Header:

在本 C#教程中,我们将了解 如何 使用 CSharp 编程语言更改 Datagridview 标题文本大小、文本颜色、字体名称、背景颜色、文本对齐方式 。

项目源代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WindowsFormsApplication1

{

public partial class Form17 : Form

{

public Form17()

{

InitializeComponent();

}

private void Form17_Load(object sender, EventArgs e)

{

// populate datagridview from datatable

DataTable table = new DataTable();

// add columns to datatable

table.Columns.Add("Id", typeof(int));

table.Columns.Add("First Name", typeof(string));

table.Columns.Add("Last Name", typeof(string));

table.Columns.Add("Age", typeof(int));

// add rows to datatable

table.Rows.Add(1, "First A", "Last A", 10);

table.Rows.Add(2, "First B", "Last B", 20);

table.Rows.Add(3, "First C", "Last C", 30);

table.Rows.Add(4, "First D", "Last D", 40);

table.Rows.Add(5, "First E", "Last E", 50);

table.Rows.Add(6, "First F", "Last F", 60);

table.Rows.Add(7, "First G", "Last G", 70);

table.Rows.Add(8, "First H", "Last H", 80);

dataGridView1.DataSource = table;

/*

dataGridView1.Columns[0].HeaderCell.Style.Font = new Font("Tahoma", 18, FontStyle.Bold);

dataGridView1.Columns[1].HeaderCell.Style.Font = new Font("Tahoma", 22, FontStyle.Italic);

dataGridView1.Columns[2].HeaderCell.Style.Font = new Font("Tahoma", 25, FontStyle.Strikeout);

dataGridView1.Columns[3].HeaderCell.Style.Font = new Font("Tahoma", 28, FontStyle.Underline);

*/

dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 25, FontStyle.Bold);

/*

dataGridView1.Columns[0].HeaderCell.Style.ForeColor = Color.Red;

dataGridView1.Columns[1].HeaderCell.Style.ForeColor = Color.Green;

dataGridView1.Columns[2].HeaderCell.Style.ForeColor = Color.Blue;

dataGridView1.Columns[3].HeaderCell.Style.ForeColor = Color.Brown;

*/

dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue;

/*

dataGridView1.Columns[0].HeaderCell.Style.BackColor = Color.White;

dataGridView1.Columns[1].HeaderCell.Style.BackColor = Color.Yellow;

dataGridView1.Columns[2].HeaderCell.Style.BackColor = Color.AliceBlue;

dataGridView1.Columns[3].HeaderCell.Style.BackColor = Color.Aqua;

*/

dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Yellow;

// center text

dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;

dataGridView1.EnableHeadersVisualStyles = false;

}

}

}

输出:

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

相关推荐
脑电信号要分类6 小时前
将多张图片拼接成一个pdf文件输出
pdf·c#·apache
njsgcs6 小时前
c# solidworks 折弯系数检查
开发语言·c#
格林威7 小时前
工业相机图像采集:Grab Timeout 设置建议——拒绝“假死”与“丢帧”的黄金法则
开发语言·人工智能·数码相机·计算机视觉·c#·机器视觉·工业相机
唐青枫8 小时前
C#.NET SignalR + Redis Backplane 深入解析:多节点部署与跨实例消息同步
c#·.net
FL162386312920 小时前
[C#][winform]segment-anything分割万物部署onnx模型一键抠图演示
开发语言·c#
love530love1 天前
OpenClaw 手机直连配置全流程
人工智能·windows·python·智能手机·c#·agent·openclaw
bcbobo21cn1 天前
C# byte类型和byte数组的使用
开发语言·c#·字节数组·byte类型
月巴月巴白勺合鸟月半1 天前
一次PDF文件的处理(一)
pdf·c#
大鹏说大话1 天前
Java 锁膨胀机制深度解析:从偏向锁到重量级锁的进化之路
开发语言·c#
武藤一雄1 天前
WPF处理耗时操作的7种方法
microsoft·c#·.net·wpf