c#设置DataGridView样式、添加行号的方法
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows.Forms; 6 using System.Drawing; 7 8 namespace Logistics 9 { 10 /// <summary> 11 /// 设置DataGridView的样式 12 /// </summary> 13 public class DataGridViewStyle 14 { 15 /// <summary> 16 /// 普通的样式 17 /// </summary> 18 public void DgvStyle1(DataGridView dgv) 19 { 20 //奇数行的背景色 21 dgv.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); 22 dgv.AlternatingRowsDefaultCellStyle.SelectionForeColor = System.Drawing.Color.Blue; 23 dgv.AlternatingRowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(255))))); 24 dgv.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 25 //默认的行样式 26 dgv.RowsDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); 27 dgv.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(255))))); 28 dgv.RowsDefaultCellStyle.SelectionForeColor = System.Drawing.Color.Blue; 29 //数据网格颜色 30 dgv.GridColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); 31 //列标题的宽度 32 dgv.ColumnHeadersHeight = 28; 33 } 34 /// <summary> 35 /// 凹凸样式 36 /// </summary> 37 /// 需要手动设置this.RowTemplate.DividerHeight = 2; 38 public void DgvStyle2(DataGridView dgv) 39 { 40 dgv.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.Sunken; 41 //列标题的边框样式 42 dgv.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Sunken; 43 dgv.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 44 dgv.ColumnHeadersDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); 45 dgv.ColumnHeadersHeight = 28; 46 //行的边框样式 47 dgv.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Sunken; 48 dgv.DefaultCellStyle.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 49 dgv.RowTemplate.DividerHeight = 1; 50 //禁止当前默认的视觉样式 51 dgv.EnableHeadersVisualStyles = false; 52 } 53 54 55 /// <summary> 56 /// 给DataGridView添加行号 57 /// </summary> 58 /// <param name="dgv"></param> 59 /// <param name="e"></param> 60 public static void DgvRowPostPaint(DataGridView dgv, DataGridViewRowPostPaintEventArgs e) 61 { 62 try 63 { 64 //添加行号 65 SolidBrush v_SolidBrush = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor); 66 int v_LineNo = 0; 67 v_LineNo = e.RowIndex + 1; 68 string v_Line = v_LineNo.ToString(); 69 e.Graphics.DrawString(v_Line, e.InheritedRowStyle.Font, v_SolidBrush, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + 5); 70 } 71 catch (Exception ex) 72 { 73 MessageBox.Show("添加行号时发生错误,错误信息:" + ex.Message, "操作失败"); 74 } 75 } 76 77 } 78 }