我要做一个显示工资表的。 需要双层表头。 初学者,不会搞,看了些例子,但都不完善,有的太复杂看不太懂。
哪位可以给一个简单易用,适合初学者的 例子。 感谢啦! 希望能够详细的。
奉上全部积分。
如果发帖不方便,请发我邮箱([email protected])。 谢谢啦!!
哪位可以给一个简单易用,适合初学者的 例子。 感谢啦! 希望能够详细的。
奉上全部积分。
如果发帖不方便,请发我邮箱([email protected])。 谢谢啦!!
原理:重写 DatagridviewPainting.表头有移动的时候,Refresh().
步骤: 1、确定你要分层的的列
2、把列之间的竖线去掉。
3、在这些列表头的中间横向画一条分割线
4、再给每列的右侧分别画竖线。
5、计算双层表头各个字符串的位置,写出来。
6、其中用到的技术细节请查看 MSDN.
7、计算字符串位置的时候要看仔细了,不然就乱了。
这些问题不难,就是很麻烦。不怕麻烦就难做出来。
google一下一大把!
http://www.lwolf.cn/blog/article/code/322.htm
对于datagridview实现各种效果的源代码
http://www.cnblogs.com/peterzb/archive/2009/05/29/1491891.html
另外还有其他控件的例子,希望对你有帮助!
int left = 0;
int height = 0;
int width1 = 0;
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
#region 重绘datagridview表头
DataGridView dgv = (DataGridView)(sender);
if (e.RowIndex == -1 && (e.ColumnIndex == 18 || e.ColumnIndex == 19)) //在第三和第四列上加了个表头
{ //e.CellStyle.Font = new Font(dataGridView1.DefaultCellStyle.Font, FontStyle.Bold);
//e.CellStyle.WrapMode = DataGridViewTriState.True;
if (e.ColumnIndex == 18)
{
top = e.CellBounds.Top;
left = e.CellBounds.Left;
height = e.CellBounds.Height;
width1 = e.CellBounds.Width;
}
int width2 = this.dataGridView1.Columns[19].Width;
Rectangle rect = new Rectangle(left, top, width1 + width2 , e.CellBounds.Height);
using (Brush backColorBrush = new SolidBrush(Color.White))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect);
} using (Pen gridLinePen = new Pen(dgv.GridColor))
{
e.Graphics.DrawLine(gridLinePen, left, top, left + width1 + width2, top);
e.Graphics.DrawLine(gridLinePen, left, top + height / 2, left + width1 + width2, top + height / 2); // 中间线
e.Graphics.DrawLine(gridLinePen, left + width1, top + height / 2, left + width1, top + height);// 短竖线
e.Graphics.DrawLine(gridLinePen, (left + width1+width2 )*999/1000, top , (left + width1+width2)*999/1000 , top + height);
e.Graphics.DrawLine(gridLinePen, left, top + height* 99/100, left + width1 + width2, top + height* 99/100);
//计算绘制字符串的位置
string columnValue = "扣费";
SizeF sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
float lstr = (width1 + width2 - sf.Width) / 2;
float rstr = (height / 2 - sf.Height) / 2;
//画出文本框 if (columnValue != "")
{
e.Graphics.DrawString(columnValue, e.CellStyle.Font,
new SolidBrush(e.CellStyle.ForeColor),
left + lstr,
top + rstr,
StringFormat.GenericDefault);
}
//计算绘制字符串的位置
columnValue = "餐费";
sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
lstr = (width1 - sf.Width) / 2;
rstr = (height / 2 - sf.Height) / 2;
//画出文本框 if (columnValue != "")
{
e.Graphics.DrawString(columnValue, e.CellStyle.Font,
new SolidBrush(e.CellStyle.ForeColor),
left + lstr,
top + height / 2 + rstr,
StringFormat.GenericDefault);
}
//计算绘制字符串的位置
columnValue = "保险";
sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
lstr = (width2 - sf.Width) / 2;
rstr = (height / 2 - sf.Height) / 2;
//画出文本框 if (columnValue != "")
{
e.Graphics.DrawString(columnValue, e.CellStyle.Font,
new SolidBrush(e.CellStyle.ForeColor),
left + width1 + lstr,
top + height / 2 + rstr,
StringFormat.GenericDefault);
} }
e.Handled = true;
}