DataGridView的paint方法来绘制一行”假”Row来显示(转)
2007-10-29 10:59 TTlive 阅读(267) 评论(0) 编辑 收藏 举报我们在用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
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
cellLocX += this.GetColumnDisplayRectangle(j, false).Width;
}
}
// draw caption string
g.DrawString("Rax", new Font(new FontFamily("Verdana"), 8), Brushes.Black, 4, LocY + 6, sf);
}
}
}
}