记WinForm DataGridView 表格显示的不同风格

  • 第一种风格:
复制代码
 1         /// <summary>
 2         /// 第一种样式
 3         /// </summary>        
 4         public void DgvStyle1(DataGridView dgv)
 5         {
 6             //奇数行的背景色
 7             dgv.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
 8             dgv.AlternatingRowsDefaultCellStyle.SelectionForeColor = System.Drawing.Color.Blue;
 9             dgv.AlternatingRowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
10             dgv.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
11             //默认的行样式
12             dgv.RowsDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
13             dgv.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
14             dgv.RowsDefaultCellStyle.SelectionForeColor = System.Drawing.Color.Blue;
15             //数据网格颜色
16             dgv.GridColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
17             //列标题的宽度
18             dgv.ColumnHeadersHeight = 30;
19         }
复制代码
  • 第二种风格:
复制代码
 1  /// <summary>
 2         /// 凹凸样式
 3         /// </summary>
 4         /// 需要手动设置this.RowTemplate.DividerHeight = 2;    
 5         public void DgvStyle2(DataGridView dgv)
 6         {
 7             //奇数行的背景色
 8             // this.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
 9             //单元格边框样式
10             dgv.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.Sunken;
11             //列标题的边框样式
12             dgv.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Sunken;
13             dgv.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
14             dgv.ColumnHeadersDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
15             dgv.ColumnHeadersHeight = 28;
16             //行的边框样式
17             dgv.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Sunken;
18             dgv.DefaultCellStyle.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
19             //this.DefaultCellStyle.ForeColor = System.Drawing.Color.Black;
20             //this.DefaultCellStyle.BackColor = System.Drawing.SystemColors.ButtonFace;
21             dgv.RowTemplate.DividerHeight = 1;
22             ////禁止当前默认的视觉样式
23             dgv.EnableHeadersVisualStyles = false;
24 
25             //自动调整列宽
26             // this.AutoResizeColumns();
27         }
复制代码
  • 第三种风格:
    复制代码
     1         /// <summary>
     2         /// 华丽的样式
     3         /// </summary>
     4         public void DgvStyle3(DataGridView dgv)
     5         {
     6             //未显示数据时的背景色
     7             dgv.BackgroundColor = System.Drawing.SystemColors.ButtonFace;
     8             //显示数据时的背景色
     9             dgv.RowsDefaultCellStyle.BackColor = System.Drawing.Color.Black;
    10             //数据网格颜色
    11             dgv.GridColor = System.Drawing.Color.Red;
    12             //列标题的边框样式
    13             dgv.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
    14             //行的边框样式
    15             dgv.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
    16             ////禁止当前默认的视觉样式
    17             dgv.EnableHeadersVisualStyles = false;
    18             //列标题的宽度
    19             dgv.ColumnHeadersHeight = 35;
    20 
    21             //列标题的字体颜色
    22             dgv.ColumnHeadersDefaultCellStyle.ForeColor = System.Drawing.Color.Blue;
    23             //列标题的背景颜色
    24             dgv.ColumnHeadersDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
    25             //所有数据字体的颜色
    26             int RowsCount = dgv.Columns.Count;
    27             for (int i = 0; i < RowsCount; i++)
    28             {
    29                 dgv.Columns[i].DefaultCellStyle.ForeColor = System.Drawing.Color.Yellow;
    30             }
    31         }
    复制代码

    方法调用: DgvStyle1(this.DataGridView名称); 

posted @   _杨不起  阅读(406)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示