C#重绘DataGridView行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#重绘DataGridView行相关的知识,希望对你有一定的参考价值。
1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System.Linq; 5 using System.Text; 6 using System.Windows.Forms; 7 8 namespace Metrox.WinFrom.DataGridView 9 { 10 /// <summary> 11 /// 派生DataGridView 12 /// </summary> 13 class myDataGridView : DataGridView 14 { 15 public myDataGridView() 16 { 17 18 } 19 20 public myDataGridView(Form _form) 21 { 22 this.Dock = DockStyle.Fill;//占满停靠 23 this.RowHeadersVisible = false;//隐藏行头 24 this.ColumnHeadersVisible = true;//隐藏列头 25 this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;//列样式 26 this.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;//行样式 27 this.AllowUserToAddRows = false;//隐藏添加行 28 this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//选择模式 29 this.ReadOnly = true;//只读 30 this.MultiSelect = false;//单选 31 //奇数行样式 32 this.AlternatingRowsDefaultCellStyle = new DataGridViewCellStyle 33 { 34 BackColor = Color.Silver, 35 SelectionForeColor = Color.White 36 }; 37 //行重绘事件 38 this.RowPrePaint += new DataGridViewRowPrePaintEventHandler(myDataGridView_RowPrePaint); 39 _form.Controls.Add(this); 40 } 41 /// <summary> 42 /// 重绘DataGridView行 43 /// </summary> 44 /// <param name="sender"></param> 45 /// <param name="e"></param> 46 private void myDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) 47 { 48 //要重绘的部份 49 e.PaintParts &= ~DataGridViewPaintParts.Focus; 50 //选中行时 51 if ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected) 52 { 53 //区域 54 Rectangle rowBounds = new Rectangle( 55 this.RowHeadersWidth, e.RowBounds.Top, 56 this.Columns.GetColumnsWidth( 57 DataGridViewElementStates.Visible) - 58 this.HorizontalScrollingOffset + 1, 59 e.RowBounds.Height); 60 //重绘区域 61 using (Brush backbrush = 62 new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds, 63 this.DefaultCellStyle.SelectionBackColor, 64 e.InheritedRowStyle.ForeColor, 65 System.Drawing.Drawing2D.LinearGradientMode.Horizontal)) 66 { 67 e.Graphics.FillRectangle(backbrush, rowBounds); 68 } 69 } 70 } 71 72 } 73 }
以上是关于C#重绘DataGridView行的主要内容,如果未能解决你的问题,请参考以下文章