程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#重寫DataGridView

C#重寫DataGridView

編輯:關於C語言
本文實例為大家分享了C#重寫DataGridVIEw的實例代碼,供大家參考,具體內容如下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace CRD.WinUI.Editors { public class DataGridView : System.Windows.Forms.DataGridVIEw { private bool _CellColorOnchange=false; private Color cell_color = Color.Yellow; private bool shifouhuasanjiao = true; private Color color_grid = Color.FromArgb(236, 233, 216); bool click = false; public DataGridVIEw() { this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); } protected override void OnCreateControl() { this.EnableHeadersVisualStyles = false; this.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(236, 233, 216); this.ColumnHeadersBorderStyle = DataGridVIEwHeaderBorderStyle.Raised; //this.ColumnHeadersHeight = 20; this.ColumnHeadersHeightSizeMode = DataGridVIEwColumnHeadersHeightSizeMode.DisableResizing; this.ColumnHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridVIEwContentAlignment.MiddleCenter; this.ColumnHeadersDefaultCellStyle.ForeColor = System.Drawing.SystemColors.WindowText; this.ColumnHeadersDefaultCellStyle.SelectionBackColor = System.Drawing.SystemColors.Highlight; this.ColumnHeadersDefaultCellStyle.SelectionForeColor = System.Drawing.SystemColors.HighlightText; this.RowHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridVIEwContentAlignment.MiddleLeft; this.RowHeadersDefaultCellStyle.BackColor = Color.FromArgb(236, 233, 216); this.RowHeadersDefaultCellStyle.ForeColor = System.Drawing.SystemColors.WindowText; this.RowHeadersBorderStyle = System.Windows.Forms.DataGridVIEwHeaderBorderStyle.Raised; this.DefaultCellStyle.SelectionBackColor = Color.DarkBlue; this.DefaultCellStyle.SelectionForeColor = Color.DarkSlateBlue; this.RowHeadersWidthSizeMode = DataGridVIEwRowHeadersWidthSizeMode.DisableResizing; //this.GridColor = Color.Silver;//表格點擊後顏色 表格線顏色 this.BackgroundColor = System.Drawing.SystemColors.Window; this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.AllowUserToOrderColumns = true; this.AutoGenerateColumns = true; base.OnCreateControl(); } Color defaultcolor; //移到單元格時的顏色 protected override void OnCellMouseMove(DataGridVIEwCellMouseEventArgs e) { base.OnCellMouseMove(e); try { if (_CellColorOnchange) Rows[e.RowIndex].DefaultCellStyle.BackColor = cell_color; } catch (Exception) { } } //進入單元格時保存當前的顏色 protected override void OnCellMouseEnter(DataGridVIEwCellEventArgs e) { base.OnCellMouseEnter(e); try { if (_CellColorOnchange) defaultcolor = Rows[e.RowIndex].DefaultCellStyle.BackColor; } catch (Exception) { } } //離開時還原顏色 protected override void OnCellMouseLeave(DataGridVIEwCellEventArgs e) { base.OnCellMouseLeave(e); try { if (_CellColorOnchange) Rows[e.RowIndex].DefaultCellStyle.BackColor = defaultcolor; } catch (Exception) { } } public bool CellColorOnchange { get { return _CellColorOnchange; } set { _CellColorOnchange = value; } } public Color DefaultcolorSet { get { return cell_color; } set { cell_color = value; } } public bool Shifouhua_Sanjiao { get { return shifouhuasanjiao; } set { shifouhuasanjiao = value; } } public Color Content_Grid_color { get { return color_grid; } set { color_grid = value; } } private void InitializeComponent() { ((System.ComponentModel.ISupportInitialize)(this)).BeginInit(); this.SuspendLayout(); // // DataGridVIEw // //this.RowTemplate.Height = 17; ((System.ComponentModel.ISupportInitialize)(this)).EndInit(); this.ResumeLayout(false); } //RowPostPaint protected override void OnRowPostPaint(DataGridVIEwRowPostPaintEventArgs e) { if (shifouhuasanjiao) { using (SolidBrush b = new SolidBrush(Color.Black)) { Image image = global::CRD.WinUI.PropertIEs.Resources.未標題_1; //e.Graphics.DrawString("►", e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 5, e.RowBounds.Location.Y + 4); //e.Graphics.DrawImageUnscaled(image, e.RowBounds.Location.X + 1, e.RowBounds.Location.Y + 2, 8, 13); if (click) if (e.RowIndex == this.CurrentRow.Index) { e.Graphics.DrawImageUnscaled(image, e.RowBounds.Location.X + 1, e.RowBounds.Location.Y + 2, 8, 13); } } } base.OnRowPostPaint(e); } protected override void OnRowPrePaint(DataGridVIEwRowPrePaintEventArgs e) { if (shifouhuasanjiao) { using (SolidBrush b = new SolidBrush(Color.Black)) { Image image = global::CRD.WinUI.PropertIEs.Resources.未標題_1; //e.Graphics.DrawString("►", e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 5, e.RowBounds.Location.Y + 4); //e.Graphics.DrawImageUnscaled(image, e.RowBounds.Location.X + 1, e.RowBounds.Location.Y + 2, 8, 13); } } base.OnRowPrePaint(e); } protected override void OnCellClick(DataGridVIEwCellEventArgs e) { if (e.RowIndex > -1&&this.CurrentRow.Index == e.RowIndex ) { click = true; } base.OnCellClick(e); } protected override void OnCellPainting(DataGridVIEwCellPaintingEventArgs e) { base.OnCellPainting(e); SolidBrush b = new SolidBrush(Color.FromArgb(236, 233, 216)); Pen whitePen = new Pen(color_grid, 1); if (e.ColumnIndex == -1 && e.RowIndex == -1) { using (LinearGradIEntBrush brush = new LinearGradIEntBrush(e.CellBounds, Color.Gray, Color.Gray, LinearGradIEntMode.ForwardDiagonal)) { e.Graphics.FillRectangle(b, e.CellBounds); Rectangle border = e.CellBounds; border.Offset(new Point(-1, -1)); e.Graphics.DrawRectangle(Pens.Gray, border); } e.PaintContent(e.CellBounds); e.Handled = true; } else if (e.RowIndex == -1) { //標題行 using (LinearGradIEntBrush brush = new LinearGradIEntBrush(e.CellBounds, Color.Silver, Color.Silver, LinearGradIEntMode.Vertical)) { e.Graphics.FillRectangle(b, e.CellBounds); Rectangle border = e.CellBounds; border.Offset(new Point(-1, -1)); e.Graphics.DrawRectangle(Pens.Silver, border); //e.Graphics.DrawRectangle(Pens.Black, border.X + 1, border.Y + 1, border.Width - 1, border.Height - 1); } e.PaintContent(e.CellBounds); e.Handled = true; } else if (e.ColumnIndex == -1) { //標題列 using (LinearGradIEntBrush brush = new LinearGradIEntBrush(e.CellBounds, Color.Silver, Color.Silver, LinearGradIEntMode.Horizontal)) { e.Graphics.FillRectangle(b, e.CellBounds); Rectangle border = e.CellBounds; border.Offset(new Point(-1, -1)); e.Graphics.DrawRectangle(Pens.Silver, border); //e.Graphics.DrawRectangle(Pens.Black, border.X+1,border.Y+1,border.Width-1,border.Height-1); e.Graphics.DrawString("△", Font,b,e.CellBounds.X,e.CellBounds.Y); } e.PaintContent(e.CellBounds); e.Handled = true; } else { //Color.FromArgb(193, 193, 193) Rectangle border = e.CellBounds; border.Offset(new Point(-1, -1)); e.Graphics.DrawRectangle(whitePen, border); } } } }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved