本人前几天在这个http://www.blogjava.net/xiekai-blog/archive/2011/03/18/282825.html地址上下了一个C#的显示二维列头的代码,生成之后在自己的工程里使用,效果还不错,但是后来我发现一个问题,就是当字段过多,出现横向滚动条之后,拉动滚动条的时候,新加入的第一列的显示很成问题,后来我在SCROLL事件里面增加了一个它提供的ReDrawHead方法,稍微好了一点,但还是成问题,就是那个字,它会一直重绘,而且它重绘的时候,感觉重绘的矩形是一直变动的,就是显示出来区域是多少,它就根据显示出来的区域进行重绘,结果就是,第一列的字段名称会跟随滚动条的移动而移动,感觉很不爽!我看了它的代码,应该就是在以下这一段里面进行修改
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
try
{
if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
DrawCell(e);
}
else
{
//二维表头
if (e.RowIndex == -1)
{
if (SpanRows.ContainsKey(e.ColumnIndex)) //被合并的列
{
//画边框
Graphics g = e.Graphics;
e.Paint(e.CellBounds, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border); int left = e.CellBounds.Left, top = e.CellBounds.Top + 2,
right = e.CellBounds.Right, bottom = e.CellBounds.Bottom; switch (SpanRows[e.ColumnIndex].Position)
{
case 1:
left += 2;
break;
case 2:
break;
case 3:
right -= 2;
break;
} //画上半部分底色
g.FillRectangle(new SolidBrush(this._mergecolumnheaderbackcolor ), left, top,
right - left, (bottom - top) / 2); //画中线
g.DrawLine(new Pen(this.GridColor), left, (top + bottom) / 2,
right, (top + bottom) / 2); //写小标题
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center; g.DrawString(e.Value + "", e.CellStyle.Font, Brushes.Black,
new Rectangle(left, (top + bottom) / 2, right - left, (bottom - top) / 2), sf);
left = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Left, true).Left - 2; if (left < 0) left = this.GetCellDisplayRectangle(-1, -1, true).Width;
right = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Right, true).Right - 2;
if (right < 0) right = this.Width; g.DrawString(SpanRows[e.ColumnIndex].Text, e.CellStyle.Font, Brushes.Black,
new Rectangle(left, top, right - left, (bottom - top) / 2), sf);
e.Handled = true;
}
}
}
base.OnCellPainting(e);
}
catch
{ }
}我想,还不如不要什么重绘了,在显示新字段名的地方,直接插入一个Lable算了!可我是用vb.net写代码的,C#的不懂...哪一位能帮忙解决一下,感激!!!
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
try
{
if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
DrawCell(e);
}
else
{
//二维表头
if (e.RowIndex == -1)
{
if (SpanRows.ContainsKey(e.ColumnIndex)) //被合并的列
{
//画边框
Graphics g = e.Graphics;
e.Paint(e.CellBounds, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border); int left = e.CellBounds.Left, top = e.CellBounds.Top + 2,
right = e.CellBounds.Right, bottom = e.CellBounds.Bottom; switch (SpanRows[e.ColumnIndex].Position)
{
case 1:
left += 2;
break;
case 2:
break;
case 3:
right -= 2;
break;
} //画上半部分底色
g.FillRectangle(new SolidBrush(this._mergecolumnheaderbackcolor ), left, top,
right - left, (bottom - top) / 2); //画中线
g.DrawLine(new Pen(this.GridColor), left, (top + bottom) / 2,
right, (top + bottom) / 2); //写小标题
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center; g.DrawString(e.Value + "", e.CellStyle.Font, Brushes.Black,
new Rectangle(left, (top + bottom) / 2, right - left, (bottom - top) / 2), sf);
left = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Left, true).Left - 2; if (left < 0) left = this.GetCellDisplayRectangle(-1, -1, true).Width;
right = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Right, true).Right - 2;
if (right < 0) right = this.Width; g.DrawString(SpanRows[e.ColumnIndex].Text, e.CellStyle.Font, Brushes.Black,
new Rectangle(left, top, right - left, (bottom - top) / 2), sf);
e.Handled = true;
}
}
}
base.OnCellPainting(e);
}
catch
{ }
}我想,还不如不要什么重绘了,在显示新字段名的地方,直接插入一个Lable算了!可我是用vb.net写代码的,C#的不懂...哪一位能帮忙解决一下,感激!!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货