protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e) { //采用双缓冲技术的控件必需的设置 this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.UserPaint, true); try { if (e.RowIndex > -1 && e.ColumnIndex > -1) { DrawCell(e); if (e.ColumnIndex == 0) { e.CellStyle.BackColor = gridColumnBackColor; } } else { //二维表头 // 绘制表头(标题样式) if (e.RowIndex == -1) { // 字体 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; // 画表头边框 Graphics g = e.Graphics; e.Paint(e.CellBounds, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border); int left = e.CellBounds.Left, top = e.CellBounds.Top, right = e.CellBounds.Right, bottom = e.CellBounds.Bottom; // 两个标题背景色 g.FillRectangle(new SolidBrush(gridHeadBackColor /*this._mergecolumnheaderbackcolor*/), left, top, right - left, bottom - top); if (e.ColumnIndex == 0) { g.DrawString(e.Value.ToString() /*工艺号*/, bigFont, Brushes.White, new Rectangle(left, top, right - left, bottom - top), sf); } // 绘制上下两层标题 if (SpanRows.ContainsKey(e.ColumnIndex)) //被合并的列 { /* * switch (SpanRows[e.ColumnIndex].Position) * { * case 1: * left += 1; * break; * * case 2: * break; * * case 3: * right -= 1; * break; * } */ // 画中线(横线) g.DrawLine(new Pen(this.GridColor), left, (top + bottom) / 2, right, (top + bottom) / 2); // 画竖线 if (e.ColumnIndex % 2 == 1) { g.DrawLine(new Pen(this.GridColor), left - 1, top, left - 1, bottom); } else { g.DrawLine(new Pen(this.GridColor), left - 1, (bottom - top) / 2 + 3, left - 1, bottom); } // 小标题 string eValue = "时间"; if (e.ColumnIndex % 2 != 0) { eValue = "温度"; } g.DrawString(eValue /*e.Value*/, e.CellStyle.Font, Brushes.White, new Rectangle(left, (top + bottom) / 2, right - left, (bottom - top) / 2), sf); left = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Left, true).Left - 2; if (left < 0) { left = this.GetCellDisplayRectangle(-1, -1, true).Width; } right = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Right, true).Right - 2; if (right < 0) { right = this.Width; } // 大标题 g.DrawString(SpanRows[e.ColumnIndex].Text, bigFont, Brushes.White, new Rectangle(left, top, right - left, (bottom - top) / 2), sf); } e.Handled = true;// 通知系统,我已经自定义处理。 } } // 打开了文字抗锯齿 e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; base.OnCellPainting(e); } catch { } }
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e) { //added by stone e.Handled = true; e.PaintBackground(e.CellBounds, true); e.PaintContent(e.CellBounds); try { if (e.RowIndex > -1 && e.ColumnIndex > -1) { DrawCell(e); } else { //二维表头 if (e.RowIndex == -1) { if (SpanRows.ContainsKey(e.ColumnIndex)) //被合并的列 { //画边框 Graphics g = e.Graphics; e.Paint(e.CellBounds, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border); int left = e.CellBounds.Left, top = e.CellBounds.Top + 2, right = e.CellBounds.Right, bottom = e.CellBounds.Bottom; switch (SpanRows[e.ColumnIndex].Position) { case 1: left += 2; break; case 2: break; case 3: right -= 2; break; } //画上半部分底色 g.FillRectangle(new SolidBrush(this._mergecolumnheaderbackcolor), left, top, right - left, (bottom - top) / 2); //画中线 g.DrawLine(new Pen(this.GridColor), left, (top + bottom) / 2, right, (top + bottom) / 2); //写小标题 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; g.DrawString(e.Value + "", e.CellStyle.Font, Brushes.Black, new Rectangle(left, (top + bottom) / 2, right - left, (bottom - top) / 2), sf); left = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Left, true).Left - 2; if (left < 0) { left = this.GetCellDisplayRectangle(-1, -1, true).Width; } right = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Right, true).Right - 2; if (right < 0) { right = this.Width; } g.DrawString(SpanRows[e.ColumnIndex].Text, e.CellStyle.Font, Brushes.Black, new Rectangle(left, top, right - left, (bottom - top) / 2), sf); e.Handled = true; } } } base.OnCellPainting(e); } catch { } }
/// <summary> /// 清除合并的列 /// </summary> public void ClearSpanInfo() { SpanRows.Clear(); //ReDrawHead(); }