DataGridView 底部添加合计行后,怎样才能使垂直滚动条滚动到最后时,最后一行不被合计行遮挡?

heshl 2009-02-27 12:36:44

其中用了一个自定义UserDataGridViewTextBoxColumn和自定义的一个类(存储合计列参数),太长了,在这里贴不上去,有需要我再贴上去。

还没有写完,现在急需要解决的是怎么样才能让垂直滚动条滚动到最后时,最后一行数据不被合计行遮挡?
...全文
559 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
songqifakevin 2011-09-16
  • 打赏
  • 举报
回复
自定义UserDataGridViewTextBoxColumn和自定义的一个类
这2个能否上传一下,谢谢!
tianmayuchuan 2010-07-23
  • 打赏
  • 举报
回复
学习学习
jywen1029 2009-10-31
  • 打赏
  • 举报
回复
同样的问题
zsyutian 2009-09-11
  • 打赏
  • 举报
回复
自定义UserDataGridViewTextBoxColumn和自定义的一个类
这两个传上来一下,我也正需要这个
heshl 2009-02-27
  • 打赏
  • 举报
回复
#region 私有方法

private void plTotal_Paint(object sender, PaintEventArgs e)
{
try
{
ResetButtomPanel();
Graphics gTmp = e.Graphics;
Pen linePen = new Pen(this.GridColor);

StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
SizeF fontsize = gTmp.MeasureString("合计", plTotal.Font);

if (this.RowHeadersVisible)
{
gTmp.DrawString("合计", plTotal.Font,new SolidBrush(plTotal.ForeColor),
new Rectangle(0, (int)((plTotal.Height - fontsize.Height) / 2) + 2,
this.RowHeadersWidth - 1, plTotal.Height - 2), sf);

gTmp.DrawLine(linePen,new Point(this.RowHeadersWidth-1,0),
new Point(this.RowHeadersWidth-1,plTotal.Height));
}
else if (this.GetFirstTotalColumnIndex() > 0)
{
int tmpIndex = this.GetFirstTotalColumnIndex();
gTmp.DrawString("合计", plTotal.Font, new SolidBrush(plTotal.ForeColor),
new Rectangle(0 - this.hScrollBarValue, this.totals[tmpIndex].Top + (int)((plTotal.Height - fontsize.Height) / 2) + 2,
this.totals[tmpIndex].Left - 1, this.totals[tmpIndex].Height), sf);
gTmp.DrawLine(linePen, new Point(this.totals[tmpIndex].Left - this.hScrollBarValue - 1, 0),
new Point(this.totals[tmpIndex].Left - this.hScrollBarValue - 1, plTotal.Height));

fontsize = gTmp.MeasureString(this.totals[tmpIndex].Value, plTotal.Font);
gTmp.DrawString(this.totals[tmpIndex].Value, plTotal.Font, new SolidBrush(plTotal.ForeColor),
new Rectangle(this.totals[tmpIndex].Left - this.hScrollBarValue, this.totals[tmpIndex].Top +
(int)((plTotal.Height - fontsize.Height) / 2) + 2, this.totals[tmpIndex].Width - 1,
this.totals[tmpIndex].Height), sf);
gTmp.DrawLine(linePen, new Point(this.totals[tmpIndex].Left
+ this.totals[tmpIndex].Width - this.hScrollBarValue + 1, 0),
new Point(this.totals[tmpIndex].Left
+ this.totals[tmpIndex].Width - this.hScrollBarValue + 1, plTotal.Height));
}


}
catch { }
}

/// <summary>
/// 获取第一个合计列的索引
/// </summary>
/// <returns></returns>
private int GetFirstTotalColumnIndex()
{
int rtn = 0;
if (this.totals == null)
{
return rtn;
}

for (int i = 0; i < this.totals.Length; i++)
{
if (this.totals[i] != null)
{
rtn = i;
break;
}
}
return rtn;
}

/// <summary>
/// 获取指定列合计值
/// </summary>
/// <param name="colIndex"></param>
/// <param name="method"></param>
/// <returns></returns>
private string GetTotalValue(int colIndex, TotalMethod method)
{
string rtn = "";

decimal sumDec = 0;
for (int i = 0; i < this.Rows.Count; i++)
{
if (this.Rows[i].Cells[colIndex].Value != null && this.Rows[i].Cells[colIndex].Value.ToString() != "")
{
try
{
sumDec += decimal.Parse(this.Rows[i].Cells[colIndex].Value.ToString());
}
catch { }
}
}

switch (method)
{
case TotalMethod.Acount:
rtn = this.Rows.Count.ToString();
break;
case TotalMethod.Avg:
if (this.Rows.Count > 0)
{
decimal decRtn = sumDec / this.Rows.Count;
rtn = decRtn.ToString();
}
break;
case TotalMethod.Sum:
rtn = sumDec.ToString();
break;
}
return rtn;
}

/// <summary>
/// 设置统计列的值,在DataGridView的Paint事件中执行
/// </summary>
private void SetTotalValue()
{
int colCount = this.ColumnCount;
if (colCount > 0)
{
int left = 0;
if (this.RowHeadersVisible)
{
left += this.RowHeadersWidth;
}
totals = new TotalValue[colCount];
for (int i = 0; i < colCount; i++)
{
if (this.Columns[i].CellType == typeof(UserDataGridViewTextBoxCell))
{
UserDataGridViewTextBoxColumn totalColumn = this.Columns[i] as UserDataGridViewTextBoxColumn;
if (totalColumn.IsTotalColumn && totalColumn.TotalMethod != TotalMethod.None)
{
totals[i] = new TotalValue();
totals[i].Left = left + 1;
totals[i].Top = 1;
totals[i].Height = this.plTotal.Height - 2;
totals[i].Width = this.Columns[i].Width - 2;
totals[i].Index = i;
totals[i].Method = totalColumn.TotalMethod;
totals[i].Value = this.GetTotalValue(i, totalColumn.TotalMethod);
}
else
{
totals[i] = null;
}
}
else
{
totals[i] = null;
}
left += this.Columns[i].Width;
}
}
}

/// <summary>
/// 重置统计带区
/// </summary>
private void ResetButtomPanel()
{
if (this.HorizontalScrollBar.Visible)
{
plTotal.Top = this.Height - plTotal.Height - this.HorizontalScrollBar.Height - 1;
}
else
{
plTotal.Top = this.Height - plTotal.Height - 1;
}

if (this.BorderStyle == BorderStyle.Fixed3D)
{
plTotal.Left = 3;
plTotal.Width = this.Width - 5;
}
else
{
plTotal.Left = 1;
plTotal.Width = this.Width - 2;
}

}

#endregion 私有方法
}
heshl 2009-02-27
  • 打赏
  • 举报
回复
源码如下:
using System.ComponentModel;
using System.Globalization;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Drawing;

namespace EasySmall.GeneralClass
{
public class gdidatagridview : DataGridView
{
Panel plTotal = new Panel();
TotalValue[] totals = null;
int hScrollBarValue = 0;

public gdidatagridview()
{
plTotal.Height = this.RowTemplate.Height - 2;
plTotal.BorderStyle = BorderStyle.None;
plTotal.Paint +=new PaintEventHandler(this.plTotal_Paint);
this.Controls.Add(plTotal);
this.AutoGenerateColumns = false;
}

#region 附加属性

[Description("设置统计区域背景颜色")]
public Color TotalBackColor
{
get { return plTotal.BackColor; }
set { plTotal.BackColor = value; }
}

[Description("设置统计区域字体颜色")]
public Color TotalForeColor
{
get { return plTotal.ForeColor; }
set { plTotal.ForeColor = value; }
}

[Description("设置统计区域字体")]
public Font TotalFont
{
get { return plTotal.Font; }
set { plTotal.Font = value; }
}

[Description("设置统计区域高度")]
public int TotalHeight
{
get { return plTotal.Height; }
set
{
plTotal.Height = value;
ResetButtomPanel();
}
}

[Description("获取统计区域合计值对应的类")]
public TotalValue[] TotalValues
{
get { return this.totals; }
}

[Description("显示或隐藏统计区域")]
public bool TotalVisible
{
get { return plTotal.Visible; }
set { plTotal.Visible = value; }
}

#endregion 附加属性
#region 重写方法

protected override void OnPaint(PaintEventArgs e)
{
this.SetTotalValue();
int tmpInt = (int)(this.Height - this.plTotal.Height - this.ColumnHeadersHeight) / this.RowTemplate.Height;

int clientHeight = this.plTotal.Height + this.ColumnHeadersHeight
+ this.RowTemplate.Height * tmpInt;
this.ClientSize = new Size(this.ClientSize.Width, clientHeight);

base.OnPaint(e);
plTotal.Invalidate();
}

protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
{
base.OnCellValueChanged(e);
if (e.ColumnIndex > -1)
{
if (this.totals != null)
{
if (this.totals[e.ColumnIndex] != null)
{
this.totals[e.ColumnIndex].Value = this.GetTotalValue(e.ColumnIndex, this.totals[e.ColumnIndex].Method);
}
}
}
}

protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
ResetButtomPanel();
}

protected override void OnBorderStyleChanged(EventArgs e)
{
base.OnBorderStyleChanged(e);
ResetButtomPanel();
}

protected override void OnScroll(ScrollEventArgs e)
{
base.OnScroll(e);
if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
{
if (e.NewValue == e.OldValue)
{
this.hScrollBarValue = e.NewValue + 1;
}
else
{
this.hScrollBarValue = e.NewValue;
}
}
}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
return base.ProcessTabKey(keyData);
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}

protected override void OnCellEnter(DataGridViewCellEventArgs e)
{
if (this.Columns[e.ColumnIndex].Visible && !this.Columns[e.ColumnIndex].ReadOnly)
{
base.OnCellEnter(e);
}
else
{
SendKeys.Send("{ENTER}");
}
}

#endregion 重写方法
heshl 2009-02-27
  • 打赏
  • 举报
回复
晕死,附件没上去
liuyjcel 2009-02-27
  • 打赏
  • 举报
回复
支持楼上...
heshl 2009-02-27
  • 打赏
  • 举报
回复
唉,没人理,只好自己做了,结贴了

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧