在DataGridView最下方通过paint显示一行相关数据
2007-01-01 21:24
344 查看
我们在用DataGridView显示数据的时候是否有时会觉得它的功能并不是十分的人性,比如我们想做一个统计所有数据的量或者一列数据的综合,总感到没有地方来呈现它们,其实我们那可以很好的利用DataGridView的paint方法来绘制一行”假”Row来显示,这样既节省空间又美观大方。
![](http://images.cnblogs.com/cnblogs_com/lowtemper/a.jpg)
现在我们就开始做一个上面的例子吧!
首先我们要继承.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);
#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);
}
}
}
}
(未经本人许可谢绝转载)
![](http://images.cnblogs.com/cnblogs_com/lowtemper/a.jpg)
现在我们就开始做一个上面的例子吧!
首先我们要继承.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);
#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);
}
}
}
}
(未经本人许可谢绝转载)
相关文章推荐
- 级联两个bootstrap-table。一张表显示相关的数据,通过点击这张表的某一行,传过去对应的ID,刷新另外一张表。
- 通过DataGridView分段显示数据
- DataGridView的paint方法来绘制一行”假”Row来显示(转)
- C#中DataGridView绑定了DataTable后,通过代码修改DataGridView中的数据,总有一行(被修改过并被用户选中的行集合中索引为0的行)不能被UpDate回数据库的问题和解决办法
- 通过DataGridView分段显示数据
- DataGridView的paint方法来绘制一行”假”Row来显示(转)
- 动态的把数据显示在table中,点击某一行的删除按钮实现该行的删除(通过ajax实现页面的无刷新加载)
- 将数据库中的数据读取到数据表中,并通过datagridview显示
- 动态的把数据显示在table中,点击某一行的删除按钮实现该行的删除(通过ajax实现页面的无刷新加载)
- Gridview一行中显示两组数据的以及相关控件的特征
- 加载数据库表全部数据到GridView控件,但通过查询条件在GridView显示一行或几行数据
- winform开发,双击dataGridView中的一行数据,将数据读出显示。
- datagridview 设置列对齐及显示数据格式
- c# winform给dataGridView添加值,插入值,填值,添加数据,添加一行,插入一行数据
- 通过python直接显示json格式数据的方法
- 用javascript把同一行中内容相同的单元格合并。— — — —即GridView中没有数据时,显示“抱歉,没有数据。”
- 在sqlplus中通过数据字典按条件查询信息(针对oracle),显示未选定行
- 通过SketchRNN、PCA和t-SNE从Google QuickDraw数据集中显示矢量图的潜在空间|附源码
- 通过创建存储过程显示数据表的字段名和说明
- C#将dataGridView中显示的数据导出到Excel(超实用版)