【转】DataGridView 的重绘
来源:互联网 发布:工商银行网络银行 编辑:程序博客网 时间:2024/05/18 02:16
.net Winform 中的DataGridView 的外观有点难看。下面是重绘DataGridView的方法,可以重绘时参考:
1.重绘DataGridView
- protected override void Paint(
- Graphics graphics,
- Rectangle clipBounds,
- Rectangle cellBounds,
- int rowIndex,
- DataGridViewElementStates cellState,
- object value,
- object formattedValue,
- string errorText,
- DataGridViewCellStyle cellStyle,
- DataGridViewAdvancedBorderStyle advancedBorderStyle,
- DataGridViewPaintParts paintParts)
- {
- // Call the base class method to paint the default cell appearance.
- base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
- value, formattedValue, errorText, cellStyle,
- advancedBorderStyle, paintParts);
- // Retrieve the client location of the mouse pointer.
- Point cursorPosition =
- this.DataGridView.PointToClient(Cursor.Position);
- // If the mouse pointer is over the current cell, draw a custom border.
- if (cellBounds.Contains(cursorPosition))
- {
- Rectangle newRect = new Rectangle(cellBounds.X + 1,
- cellBounds.Y + 1, cellBounds.Width - 4,
- cellBounds.Height - 4);
- graphics.DrawRectangle(Pens.Red, newRect);
- }
- }
2.重绘DataGridView 的头部
- protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
- {
- base.OnCellPainting(e);
- if (e.ColumnIndex == -1 && e.RowIndex == -1)
- {
- using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.LightGray,
- Color.White, LinearGradientMode.ForwardDiagonal))
- {
- e.Graphics.FillRectangle(brush, 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.LightGray,
- Color.White, LinearGradientMode.Vertical))
- {
- e.Graphics.FillRectangle(brush, 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.ColumnIndex == -1)
- {
- //标题列
- using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.LightGray,
- Color.White, LinearGradientMode.Horizontal))
- {
- e.Graphics.FillRectangle(brush, 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;
- }
- }
3.重绘实现合并单元格
- #region"合并单元格的测试"
- private int? nextrow = null;
- private int? nextcol = null;
- private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
- {
- if (this.dataGridView1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
- {
- if (this.nextcol != null & e.ColumnIndex == this.nextcol)
- {
- e.CellStyle.BackColor = Color.LightBlue;
- this.nextcol = null;
- }
- if (this.nextrow != null & e.RowIndex == nextrow)
- {
- e.CellStyle.BackColor = Color.LightPink;
- this.nextrow = null;
- }
- if (e.RowIndex != this.dataGridView1.RowCount - 1)
- {
- if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
- {
- e.CellStyle.BackColor = Color.LightPink;
- nextrow = e.RowIndex + 1;
- }
- }
- }
- if (this.dataGridView1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)
- {
- if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
- {
- e.CellStyle.BackColor = Color.LightBlue;
- nextcol = e.ColumnIndex + 1;
- }
- }
- }
- //==========================
- //绘制单元格
- private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
- {
- //纵向合并
- if (this.dataGridView1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
- {
- using (
- Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
- backColorBrush = new SolidBrush(e.CellStyle.BackColor))
- {
- using (Pen gridLinePen = new Pen(gridBrush))
- {
- // 擦除原单元格背景
- e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
- ////绘制线条,这些线条是单元格相互间隔的区分线条,
- ////因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条
- if (e.RowIndex != this.dataGridView1.RowCount - 1)
- {
- if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
- {
- e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
- e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线
- //绘制值
- if (e.Value != null)
- {
- e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
- Brushes.Crimson, e.CellBounds.X + 2,
- e.CellBounds.Y + 2, StringFormat.GenericDefault);
- }
- }
- }
- else
- {
- e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
- e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线
- //绘制值
- if (e.Value != null)
- {
- e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
- Brushes.Crimson, e.CellBounds.X + 2,
- e.CellBounds.Y + 2, StringFormat.GenericDefault);
- }
- }
- //右侧的线
- e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
- e.CellBounds.Top, e.CellBounds.Right - 1,
- e.CellBounds.Bottom - 1);
- e.Handled = true;
- }
- }
- }
- //横向合并
- if (this.dataGridView1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)
- {
- using (
- Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
- backColorBrush = new SolidBrush(e.CellStyle.BackColor))
- {
- using (Pen gridLinePen = new Pen(gridBrush))
- {
- // 擦除原单元格背景
- e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
- if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
- {
- //右侧的线
- e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
- e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
- //绘制值
- if (e.Value != null)
- {
- e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
- Brushes.Crimson, e.CellBounds.X + 2,
- e.CellBounds.Y + 2, StringFormat.GenericDefault);
- }
- }
- //下边缘的线
- e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
- e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
- e.Handled = true;
- }
- }
- }
- }
- #endregion
0 0
- 【转】DataGridView 的重绘
- DataGridView 的重绘
- [WinForm]重绘DataGridView的序号
- datagridview单元格重绘
- 重绘DataGridView行
- 【机房重构】datagridview的使用
- 【机房个人重构】datagridview的使用
- DataGridView控件重畫
- datagridView 去重
- 【机房重构】——DataGridView控件的使用
- 【C#机房重构】 DataGridView控件的使用
- 机房重构—DataGridView控件的使用
- DataGridView重绘单元格中某些字体颜色,大小
- DataGridView重绘单元格中某些字体颜色,大小
- datagridview横向纵向合并单元格,重绘实现
- Winfrom 重绘dataGridView边框颜色(SourceGrid、Panel通用)
- C#操作Excel数据导入和dataGridView重绘
- 【C#】DataGridView<机房重构>
- 浅析linux下的/etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc文件
- swift写的第一个helloWorld 变量和常量相对oc的区别
- Unity3D如何接入第三方的SDK - Android篇
- copy_to_user和copy_from_user两个函数的分析
- Utilities之auto_ptr
- 【转】DataGridView 的重绘
- DOM解析XML(二)
- try catch多个catch捕获问题
- hdu2065--红色病毒
- Unlities之numeric_limits
- c++ double类型的误差
- [iOS Crash文件分析]-如何使用symbolicatecrash工具
- 烤鸭的gerrit使用总结
- 单元测试之覆盖率浅谈