DataGridView的paint方法来绘制一行”假”Row来显示(转)
来源:互联网 发布:启智星幼儿教育软件 编辑:程序博客网 时间:2024/04/29 07:58
我们在用DataGridView显示数据的时候是否有时会觉得它的功能并不是十分的人性,比如我们想做一个统计所有数据的量或者一列数据的综合,总感到没有地方来呈现它们,其实我们那可以很好的利用DataGridView的paint方法来绘制一行”假”Row来显示,这样既节省空间又美观大方。
首先我们要继承.net自带的强大的DataGridView
1. 提供一个枚举类型BottomAlign用来显示相关值排列的对齐方式(left,right,center),并且在类中定义一个BottomAlign类型的BottomAlignment属性
2. 重写DataGridView的OnPaint方法
具体代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace RaxComponent
...{
public enum BottomAlign ...{ left, right, center }
public class RaxDataGridView : DataGridView
...{
private BottomAlign _BottomAlignment = BottomAlign.left;
[Category("Rax")]
public BottomAlign BottomAlignment
...{
get
...{
return _BottomAlignment;
}
set
...{
_BottomAlignment = value;
}
}
protected override void OnPaint(PaintEventArgs e)
...{
base.OnPaint(e);
Graphics g;
if (e == null)
...{
g = Graphics.FromHwnd(this.Handle);
}
else
...{
g = e.Graphics;
}
SolidBrush myBrush1 = new SolidBrush(SystemColors.Control);
SolidBrush myBrush2 = new SolidBrush(Color.IndianRed);
Pen pen1 = new Pen(Brushes.White, 1);
if (this.Rows.Count > 0 && this.Rows[this.Rows.Count - 1].Displayed)
...{
int LocY = this.GetRowDisplayRectangle(this.Rows.Count - 1, true).Location.Y + this.Rows[this.Rows.Count - 1].Height;
//draw caption
g.FillRectangle(myBrush1, 2, LocY, this.RowHeadersWidth - 2, 23);
//caption's top line
g.DrawLine(pen1, new Point(2, LocY), new Point(this.RowHeadersWidth - 1, LocY));
// caption's left line
g.DrawLine(pen1, new Point(2, LocY), new Point(2, LocY + 23));
//draw cells
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Near;
int cellLocX = this.RowHeadersWidth + 1;
int i = this.Columns.Count;
for (int j = 0; j < i; j++)
...{
if (this.Columns[j].Displayed)
...{
int cellTextLocX = cellLocX;
g.FillRectangle(myBrush2, cellLocX, LocY, this.GetColumnDisplayRectangle(j, false).Width - 1, 23);
DrawString#region DrawString
if (this.GetColumnDisplayRectangle(j, false).Width == this.Columns[j].Width)
...{
int cellWidth = this.GetColumnDisplayRectangle(j, false).Width;
try
...{
string strFmValue = this.Columns[j].DataPropertyName + "列相关值";
switch (this.BottomAlignment)
...{
case BottomAlign.right:
cellTextLocX = cellTextLocX + cellWidth - (int)g.MeasureString(strFmValue, new Font(new FontFamily("Verdana"), 8), cellWidth).Width;
break;
case BottomAlign.center:
cellTextLocX = cellTextLocX + cellWidth / 2 - (int)(g.MeasureString(strFmValue, new Font(new FontFamily("Verdana"), 8), cellWidth).Width / 2);
break;
}
g.DrawString(strFmValue, new Font(new FontFamily("Verdana"), 8), Brushes.Black, cellTextLocX, LocY + 6, sf);
}
catch
...{
g.DrawString("", new Font(new FontFamily("Verdana"), 8), Brushes.Black, cellTextLocX, LocY + 6, sf);
}
}
#endregion
cellLocX += this.GetColumnDisplayRectangle(j, false).Width;
}
}
// draw caption string
g.DrawString("Rax", new Font(new FontFamily("Verdana"), 8), Brushes.Black, 4, LocY + 6, sf);
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace RaxComponent
...{
public enum BottomAlign ...{ left, right, center }
public class RaxDataGridView : DataGridView
...{
private BottomAlign _BottomAlignment = BottomAlign.left;
[Category("Rax")]
public BottomAlign BottomAlignment
...{
get
...{
return _BottomAlignment;
}
set
...{
_BottomAlignment = value;
}
}
protected override void OnPaint(PaintEventArgs e)
...{
base.OnPaint(e);
Graphics g;
if (e == null)
...{
g = Graphics.FromHwnd(this.Handle);
}
else
...{
g = e.Graphics;
}
SolidBrush myBrush1 = new SolidBrush(SystemColors.Control);
SolidBrush myBrush2 = new SolidBrush(Color.IndianRed);
Pen pen1 = new Pen(Brushes.White, 1);
if (this.Rows.Count > 0 && this.Rows[this.Rows.Count - 1].Displayed)
...{
int LocY = this.GetRowDisplayRectangle(this.Rows.Count - 1, true).Location.Y + this.Rows[this.Rows.Count - 1].Height;
//draw caption
g.FillRectangle(myBrush1, 2, LocY, this.RowHeadersWidth - 2, 23);
//caption's top line
g.DrawLine(pen1, new Point(2, LocY), new Point(this.RowHeadersWidth - 1, LocY));
// caption's left line
g.DrawLine(pen1, new Point(2, LocY), new Point(2, LocY + 23));
//draw cells
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Near;
int cellLocX = this.RowHeadersWidth + 1;
int i = this.Columns.Count;
for (int j = 0; j < i; j++)
...{
if (this.Columns[j].Displayed)
...{
int cellTextLocX = cellLocX;
g.FillRectangle(myBrush2, cellLocX, LocY, this.GetColumnDisplayRectangle(j, false).Width - 1, 23);
DrawString#region DrawString
if (this.GetColumnDisplayRectangle(j, false).Width == this.Columns[j].Width)
...{
int cellWidth = this.GetColumnDisplayRectangle(j, false).Width;
try
...{
string strFmValue = this.Columns[j].DataPropertyName + "列相关值";
switch (this.BottomAlignment)
...{
case BottomAlign.right:
cellTextLocX = cellTextLocX + cellWidth - (int)g.MeasureString(strFmValue, new Font(new FontFamily("Verdana"), 8), cellWidth).Width;
break;
case BottomAlign.center:
cellTextLocX = cellTextLocX + cellWidth / 2 - (int)(g.MeasureString(strFmValue, new Font(new FontFamily("Verdana"), 8), cellWidth).Width / 2);
break;
}
g.DrawString(strFmValue, new Font(new FontFamily("Verdana"), 8), Brushes.Black, cellTextLocX, LocY + 6, sf);
}
catch
...{
g.DrawString("", new Font(new FontFamily("Verdana"), 8), Brushes.Black, cellTextLocX, LocY + 6, sf);
}
}
#endregion
cellLocX += this.GetColumnDisplayRectangle(j, false).Width;
}
}
// draw caption string
g.DrawString("Rax", new Font(new FontFamily("Verdana"), 8), Brushes.Black, 4, LocY + 6, sf);
}
}
}
}
- DataGridView的paint方法来绘制一行”假”Row来显示(转)
- paint()方法中Graphics实例对象是怎么来的
- paint()方法中Graphics实例对象是怎么来的
- paint()方法中Graphics实例对象是怎么来的
- [LeetCode]500. Keyboard Row(输出在键盘一行上能敲出来的单词)
- winform 中要让 DataGridView 显示出每行的行号来
- [VB.NET]在vb.net中怎样来改变datagridview上一行的颜色??
- 改变DataGridView列显示顺序,通过设置DisplayIndex来改变显示的顺序
- C++ Builder 使用Canvas的方法来绘制图形对象
- DataGridView最后一行不排序的方法
- DataGridView使用固定行来显示统计信息
- DataGridView显示时一行都不选
- Qt-Quick中设置组件风格、显示一张图片、在一行显示多张图片的方法、绘制图片方法...
- 不用alert弹出窗口的方法来显示验证信息
- 通过原生的方法 来实现 隐藏显示
- JFC 中绘制方法 paint(Graphics g) 和 paintComponent(Graphics g) 的区别(转)
- 使用xml来绘制简单的图像
- 使用doublebuffered来解决VB.net中datagridview数据显示和滚动慢和卡顿的问题
- 知识积累(七)——Acegi (version1.0.4)中文参考手册——第四章 信道安全
- 设计模式-Abstract Factory模式
- 通过自然选择产生的进化
- 传染病的体征和症状
- 知识积累(八)——Acegi (version1.0.4)中文参考手册——第五章 标签库
- DataGridView的paint方法来绘制一行”假”Row来显示(转)
- shell学习之- grep 用法简明分析
- 不断升级的军备竞赛
- 创伤
- o,
- struct和 union用 sizeof 看字节对齐,以及__declspec( align( # ) ) 和 #pragma pack()的使用方式
- 新的、老的、无所不在的毒素
- life365是骗 子
- ReportX技巧(4)用ReportX的脚本功能连接数据库