public partial class WDataGridView : DataGridView { public WDataGridView() { InitializeComponent(); SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true); } public WDataGridView(IContainer container) { container.Add(this); InitializeComponent(); } private Color m_colorAlternatingRowsBack = Color.FromArgb(255, 255, 255); [Description("奇数行颜色")] public Color ColorAlternatingRowsBack { get { return m_colorAlternatingRowsBack; } set { m_colorAlternatingRowsBack = value; AlternatingRowsDefaultCellStyle.BackColor = value; } } private Color m_colorDefaultRowsBack = Color.FromArgb(235, 235, 235); [Description("默认行颜色")] public Color ColorDefaultRowsBack { get { return m_colorDefaultRowsBack; } set { m_colorDefaultRowsBack = value; RowsDefaultCellStyle.BackColor = value; } } private Color m_colorDefaultRowsFore = Color.FromArgb(0, 0, 0); [Description("默认行字体颜色")] public Color ColorDefaultRowsFore { get { return m_colorDefaultRowsFore; } set { m_colorDefaultRowsFore = value; RowsDefaultCellStyle.ForeColor = value; } } private Color m_colorDefaultRowsSelectBack = Color.FromArgb(100, 255, 255); [Description("选中行颜色")] public Color ColorDefaultRowsSelectBack { get { return m_colorDefaultRowsSelectBack; } set { m_colorDefaultRowsSelectBack = value; RowsDefaultCellStyle.SelectionBackColor = value; } } private Color m_colorDefaultRowsSelectFore = Color.FromArgb(0, 0, 0); [Description("选中行字体颜色")] public Color ColorDefaultRowsSelectFore { get { return m_colorDefaultRowsSelectFore; } set { m_colorDefaultRowsSelectFore = value; RowsDefaultCellStyle.SelectionForeColor = value; } } private void DgvStyle(DataGridView dgv) { dgv.RowHeadersVisible = false; //奇数行的背景色 dgv.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(255, 255, 255); //默认的行样式 dgv.RowsDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(180, 180, 180); dgv.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.FromArgb(0, 255, 255); dgv.RowsDefaultCellStyle.SelectionForeColor = System.Drawing.Color.FromArgb(0, 0, 0); //数据网格颜色 //dgv.GridColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); dgv.GridColor = System.Drawing.Color.Gray; //列标题的宽度 dgv.ColumnHeadersHeight = 30; // dgv.ReadOnly = true; //dgv.AllowUserToResizeColumns = false; //dgv.AllowUserToResizeRows = false; this.AllowDrop = true; //this.MultiSelect = false; this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//点击单元格选中整行 } private bool m_bDrawBorder = true; [Description("是否绘制边框")] public bool bDrawBorder { get { return m_bDrawBorder; } set { m_bDrawBorder = value; Invalidate(); } } private Color m_clHeadStart = Color.FromArgb(242, 243, 245); [Description("标头背景渐变开始颜色")] public Color HeadStart { get { return m_clHeadStart; } set { m_clHeadStart = value; Invalidate(); } } private Color m_clHeadStop = Color.FromArgb(242, 243, 245); [Description("标头背景渐变结束颜色")] public Color HeadStop { get { return m_clHeadStop; } set { m_clHeadStop = value; Invalidate(); } } private Color m_clHeadText = Color.Black; [Description("标头字体颜色")] public Color ColorHeadText { get { return m_clHeadText; } set { m_clHeadText = value; Invalidate(); } } private DataGridViewContentAlignment m_CellTextAlign = DataGridViewContentAlignment.MiddleLeft; [Description("单元格文本对齐方式")] public DataGridViewContentAlignment CellTextAlign { get { return m_CellTextAlign;} set { m_CellTextAlign = value; Invalidate();} } /// <summary> /// 绘制单元格事件 /// </summary> /// <param name="e"></param> protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e) { base.OnCellPainting(e); e.CellStyle.Alignment = m_CellTextAlign;//单元格内容集中 bool bDrawBorder = true; if (e.ColumnIndex == -1 && e.RowIndex == -1) { using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, m_clHeadStart, m_clHeadStop, LinearGradientMode.ForwardDiagonal)) { e.Graphics.FillRectangle(brush, e.CellBounds); Rectangle border = e.CellBounds; border.Offset(new Point(-1, -1)); if (m_bDrawBorder) e.Graphics.DrawRectangle(Pens.Gray, border);//绘制边框 } //e.PaintContent(e.CellBounds); //绘制标头 if (e.Value != null) { StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; e.Graphics.DrawString(e.Value.ToString(), new Font("微软雅黑", 11), Brushes.Black, e.CellBounds, sf); } e.Handled = true; } else if (e.RowIndex == -1) { //标题行 using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, m_clHeadStart, m_clHeadStop, LinearGradientMode.Vertical)) { e.Graphics.FillRectangle(brush, e.CellBounds); Rectangle border = e.CellBounds; border.Offset(new Point(-1, -1)); if (m_bDrawBorder) e.Graphics.DrawRectangle(Pens.Gray, border);//绘制边框 } //e.PaintContent(e.CellBounds); if (e.Value != null) { StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; e.Graphics.DrawString(e.Value.ToString(), new Font("微软雅黑", 11), new SolidBrush(m_clHeadText), e.CellBounds, sf); } e.Handled = true; } else if (e.ColumnIndex == -1) { //标题列 using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, m_clHeadStart, m_clHeadStop, LinearGradientMode.Horizontal)) { e.Graphics.FillRectangle(brush, e.CellBounds); Rectangle border = e.CellBounds; border.Offset(new Point(-1, -1)); if (m_bDrawBorder) e.Graphics.DrawRectangle(Pens.Gray, border); } //e.PaintContent(e.CellBounds); if (e.Value != null) { StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; e.Graphics.DrawString(e.Value.ToString(), base.Font, Brushes.White, e.CellBounds, sf); } e.Handled = true; } } private int m_nExtralColumn = 1; public int ExtralColumn { get { return m_nExtralColumn; } set { m_nExtralColumn = value; Invalidate(); } } private bool m_bDrawHorizon = true; [Description("是否绘制水分线")] public bool bDrawHorizon { get {return m_bDrawHorizon; } set { m_bDrawHorizon = value; Invalidate(); } } private bool m_bDrawVertical = false; [Description("是否绘制垂直线")] public bool bDrawVertical { get { return m_bDrawVertical; } set { m_bDrawVertical = value; Invalidate(); } } private int m_nExtralRow = 2; /// <summary> /// 绘制控件事件 /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); //return; if (ColumnCount == 0) return; Graphics g = e.Graphics; Rectangle rcClient = ClientRectangle; bool bDrawHorizon = true; //绘制水平线 bool bDrawVertical = false; using (Pen p = new Pen(GridColor)) { int nColumnIndex = 0; if (RowHeadersVisible) nColumnIndex = -1; else while (nColumnIndex < ColumnCount - 1 && !Columns[nColumnIndex].Visible) { nColumnIndex++; //隐藏列 } if (m_bDrawHorizon)//水平线 { for (int i = 0; i <= RowCount && GetCellDisplayRectangle(nColumnIndex, i - 1, false).Bottom < rcClient.Bottom; i++) { Point ptStart = new Point(GetCellDisplayRectangle(nColumnIndex, i - 1, false).X, GetCellDisplayRectangle(nColumnIndex, i - 1, false).Bottom - 1); Point ptStop = new Point(rcClient.Right - GetCellDisplayRectangle(nColumnIndex, i - 1, false).X, GetCellDisplayRectangle(nColumnIndex, i - 1, false).Bottom - 1); g.DrawLine(p, ptStart, ptStop); } if (GetCellDisplayRectangle(nColumnIndex, RowCount - 1, false).Bottom != 0) { if (rcClient.Bottom > GetCellDisplayRectangle(nColumnIndex, RowCount - 1, false).Bottom && (!GetCellDisplayRectangle(nColumnIndex, RowCount - 1, false).IsEmpty && RowCount > 0 || RowCount == 0)) { m_nExtralRow = (int)((rcClient.Bottom - GetCellDisplayRectangle(nColumnIndex, RowCount - 1, false).Bottom) * 1.0 / RowTemplate.Height); if ((rcClient.Bottom - GetCellDisplayRectangle(nColumnIndex, RowCount - 1, false).Bottom % RowTemplate.Height) != 0) m_nExtralRow++; for (int i = 1; i < m_nExtralRow; i++) { Point ptStart = new Point(GetCellDisplayRectangle(nColumnIndex, RowCount - 1, false).X, GetCellDisplayRectangle(nColumnIndex, RowCount - 1, false).Bottom + i * RowTemplate.Height - 1); Point ptStop = new Point(rcClient.Right - GetCellDisplayRectangle(nColumnIndex, RowCount - 1, false).X, GetCellDisplayRectangle(nColumnIndex, RowCount - 1, false).Bottom + i * RowTemplate.Height - 1); g.DrawLine(p, ptStart, ptStop); } } } } if (m_bDrawVertical)//垂直线 { for (int i = 0; i <= ColumnCount && GetCellDisplayRectangle(i - 1, -1, false).Right < rcClient.Right; i++) { Point ptStart = new Point((GetCellDisplayRectangle(i - 1, -1, false).Right) - 1, GetCellDisplayRectangle(nColumnIndex, -1, false).Top); Point ptStop = new Point((GetCellDisplayRectangle(i - 1, -1, false).Right) - 1, rcClient.Bottom - GetCellDisplayRectangle(nColumnIndex, -1, false).Top); g.DrawLine(p, ptStart, ptStop); } if (rcClient.Right > GetCellDisplayRectangle(ColumnCount - 1, -1, false).Right && !GetCellDisplayRectangle(ColumnCount - 1, -1, false).IsEmpty) { int nExtraWidth = (rcClient.Right - GetCellDisplayRectangle(ColumnCount - 1, -1, false).Right) / m_nExtralColumn; for (int i = 1; i < m_nExtralColumn; i++) { Point ptStart = new Point(GetCellDisplayRectangle(ColumnCount - 1, -1, false).Right + i * nExtraWidth - 1, GetCellDisplayRectangle(nColumnIndex, -1, false).Top); Point ptStop = new Point(GetCellDisplayRectangle(ColumnCount - 1, -1, false).Right + i * nExtraWidth - 1, rcClient.Bottom - GetCellDisplayRectangle(nColumnIndex, -1, false).Top); g.DrawLine(p, ptStart, ptStop); } } } } } }