您的位置:首页 > 大数据 > 人工智能

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);


}


}


}


}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐