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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM