DataGridView的paint方法来绘制一行”假”Row来显示(转)
2007-10-29 10:59
204 查看
我们在用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);
}
}
}
}
首先我们要继承.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);
}
}
}
}
相关文章推荐
- DataGridView的paint方法来绘制一行”假”Row来显示(转)
- Qt-Quick中设置组件风格、显示一张图片、在一行显示多张图片的方法、绘制图片方法...
- 用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号
- 要绘图,首先得调整画笔,待画笔调整好之后,再将图像绘制到画布上,这样才可以显示在手机屏幕上。Android 中的画笔是 Paint类,Paint 中包含了很多方法对其属性进行设置,主要方法(没有全部列
- 在DataGridView最下方通过paint显示一行相关数据
- DataGridView显示行号-RowPostPaint
- DataGridView单元格显示多行的设置方法
- Android在ListView中嵌套一个GridView时只显示一行的原因及解决方法
- C# DataGridView显示行号的三种方法
- (转)解决ScrollView嵌套ListView或者GridView导致只显示一行的方法
- 在ASP.NET中实现鼠标移动悬停在DataGrid时高亮显示这一行的方法
- DataGridView 绑定List时 属性不显示的解决方法
- datagridview中显示的数据导出到Excel——用最简单的方法解决科学计数法的问题
- ScrollView 嵌套ListView只显示一行的解决方法
- 关于ScrollView中嵌套GridVIew只显示一行解决方法以及GridView行高计算方法
- php循环table实现一行两列显示的方法
- CSS:多个DIV在同一行居中显示的一种实现方法
- Python使用matplotlib绘制多个图形单独显示的方法示例
- Android在ListView中嵌套一个GridView时只显示一行的原因及解决方法
- DataGridView显示行号的几种方法