做WinForm开发离不开一些基本的控件,作为数据列表显示控件中,其中最为重要的要数DataGridView,以前用的是一些第三方控件,提供了比较灵活和方便的功能,比如:根据某列分组统计等等,确实方便,但加载第三方控件的缺点就是加载数据太慢,打包布署后体积大,更为甚者还要收费,咱又不能用盗版,没办法自力更生才是生存之道。
DataGridView的行统计汇总功能在网络上搜索后,有一些同仁实现过(Summary DataGridView),但只是在某种特定的需求下实现了目标,要么是扩展不方便,要么是性能太差,加载一个DataGridView不知重复运行几十次代码(未做详细统计),为了一劳永逸的解决这个问题,特改写了代码,在此提供给各位同仁,欢迎斧正。
1、首先,看使用方法:
dgvProjectList.ShowSummary(new string[] { "TotalIncome", "TotalFee", "T otalCost", "SaleAmount", "RetainedProfits" });
如果不使用统计汇总,不会对现有DateGridView控件产生任何负作用,也不占用内存;"TotalIncome", "TotalFee", "TotalCost", "SaleAmount", "RetainedProfits"为要统计的各行。
效果如下:
代码如下:
1、DataGridView扩展方法定义
代码
#region显示统计列
///<summary>
///显示DataGridView的统计信息
///</summary>
///<param name="dgv"></param>
///<param name="SummaryColumns">要统计的列名称或数据源绑定列名称</pa ram>
public static void ShowSummary(this DataGridView dgv,string[] Su mmaryColumns)
{
SummaryControlContainer summaryControl = new SummaryControlC ontainer(dgv,SummaryColumns);
dgv.Controls.Add(summaryControl);
//dgv.Tag = summaryControl;
summaryControl.BringToFront();
summaryControl.Show();
}
///<summary>
///显示DataGridView的统计信息
///</summary>
///<param name="dgv"></param>
///<param name="DisplaySumRowHeader">是否显示合计行标题</param>
///<param name="SumRowHeaderText">合计列标题</param>
///<param name="SumRowHeaderTextBold">合计列标题用粗体显示</param>
///<param name="SummaryColumns">要统计的列名称或数据源绑定列名称</pa ram>
public static void ShowSummary(this DataGridView dgv, bool Displ aySumRowHeader, string SumRowHeaderText, bool SumRowHeaderTextBold, stri ng[] SummaryColumns)
{
SummaryControlContainer summaryControl = new SummaryControlC ontainer(dgv, DisplaySumRowHeader, SumRowHeaderText, SumRowHeaderTextBol d, SummaryColumns);
dgv.Controls.Add(summaryControl);
//dgv.Tag = summaryControl;
summaryControl.BringToFront();
summaryControl.Show();
}
#endregion
2、核心类定义
代码
internal class SummaryControlContainer : UserControl
{
#region公有属性
private bool _DisplaySumRowHeader;
///<summary>
///是否显示合计行标题
///</summary>
public bool DisplaySumRowHeader
{
get { return _DisplaySumRowHeader; }
set { _DisplaySumRowHeader = value; }
}
private string _SumRowHeaderText = "合计";
///<summary>
/
//合计列标题
///</summary>
public string SumRowHeaderText
{
get
{
if (_DisplaySumRowHeader)
{
return _SumRowHeaderText;
}
else
{
return string.Empty;
}
}
set
{
if (string.IsNullOrEmpty(value))
{
_SumRowHeaderText = "合计";
}
else
{
_SumRowHeaderText = value;
}
}
}
private bool _SumRowHeaderTextBold;
///<summary>
///合计列标题用粗体显示
///</summary>
public bool SumRowHeaderTextBold
{
get { return _SumRowHeaderTextBold; }
set { _SumRowHeaderTextBold = value; } }
private string[] _SummaryColumns;
///<summary>
///要统计的列名称或数据源绑定列名称
///</summary>
public string[] SummaryColumns
{
get { return _SummaryColumns; }
set
{
_SummaryColumns = value;
}
}
private string _FormatString = "F02";
public string FormatString
{
get { return _FormatString; }
set { _FormatString = value; }
}
#endregion
#region私有变量
private Hashtable sumBoxHash;
private DataGridView dgv;
private Label sumRowHeaderLabel;
#endregion
#region构造函数
public SummaryControlContainer(DataGridView dgv, string[] summar yColumns)
: this(dgv, true, "合计", false, summaryColumns)
{
}
public SummaryControlContainer(DataGridView dgv, bool displaySum RowHeader, string sumRowHeaderText,
bool sumRowHeaderTextBold, string[] summaryColumns)
{
if (dgv == null)
{
throw new Exception("DataGridView 不能为空!");
}
this.dgv = dgv;
_DisplaySumRowHeader = displaySumRowHeader;
_SumRowHeaderText = sumRowHeaderText;
_SumRowHeaderTextBold = sumRowHeaderTextBold;
_SummaryColumns = summaryColumns;
this.Visible = true;
this.Height = dgv.RowTemplate.Height;
this.Top = dgv.Height - this.Height;
this.Left = dgv.Left;
this.BackColor = dgv.RowHeadersDefaultCellStyle.BackColor;
sumBoxHash = new Hashtable();
sumRowHeaderLabel = new Label();
sumRowHeaderLabel.Height = this.Height;
sumRowHeaderLabel.Width = dgv.RowHeadersWidth;
sumRowHeaderLabel.BackColor = dgv.RowHeadersDefaultCellStyle. BackColor;
this.dgv.Resize += new EventHandler(dgv_Resize);
this.dgv.Scroll += new ScrollEventHandler(dgv_Scroll);
this.dgv.ColumnWidthChanged += new DataGridViewColumnEventHa ndler(dgv_ColumnWidthChanged);
this.dgv.RowHeadersWidthChanged += new EventHandler(dgv_RowH eadersWidthChanged);
this.dgv.RowsAdded += new DataGridViewRowsAddedEventHandler (dgv_RowsAdded);
this.dgv.RowsRemoved += new DataGridViewRowsRemovedEventHand ler(dgv_RowsRemoved);
this.dgv.CellValueChanged += new DataGridViewCellEventHandle r(dgv_CellValueChanged);
this.dgv.DataSourceChanged += new EventHandler(dgv_DataSourc eChanged);
this.dgv.ColumnAdded += new DataGridViewColumnEventHandler(d gv_ColumnAdded);
this.dgv.ColumnRemoved += new DataGridViewColumnEventHandler
(dgv_ColumnRemoved);
this.dgv.ColumnStateChanged += new DataGridViewColumnStateCh angedEventHandler(dgv_ColumnStateChanged);
this.dgv.ColumnDisplayIndexChanged += new DataGridViewColumn EventHandler(dgv_ColumnDisplayIndexChanged);
reCreateSumBoxes();
}
#endregion
#region私有方法
///<summary>
/// Checks if passed object is of type of integer
///</summary>
///<param name="o">object</param>
///<returns>true/ false</returns>
protected bool IsInteger(object o)
{
if (o is Int64)
{
return true;
}
if (o is Int32)
{
return true;
}
if (o is Int16)
{
return true;
}
return false;
}
///<summary>
/// Checks if passed object is of type of decimal/ double
///</summary>
///<param name="o">object</param>
///<returns>true/ false</returns>
protected bool IsDecimal(object o)
{
if (o is Decimal)
{
return true;
}
if (o is Single)
{
return true;
}
if (o is Double)
{
return true;
}
return false;
}
///<summary>
/// Calculate the Sums of the summary columns
///</summary>
private void calcSummaries()
{
foreach (ReadOnlyTextBox roTextBox in sumBoxHash.Values)
{
if (roTextBox.IsSummary)
{
roTextBox.Tag = 0;
roTextBox.Text = "0";
roTextBox.Invalidate();
}
}datagridview数据源
if (SummaryColumns != null && SummaryColumns.Length > 0 && s umBoxHash.Count > 0)
{
foreach (DataGridViewRow dgvRow in dgv.Rows)
{
foreach (DataGridViewCell dgvCell in dgvRow.Cells) {
foreach (DataGridViewColumn dgvColumn in sumBoxH ash.Keys)
{
if (dgvCell.OwningColumn.Equals(dgvColumn)) {
ReadOnlyTextBox sumBox = (ReadOnlyTextBo x)sumBoxHash[dgvColumn];
if (sumBox != null && sumBox.IsSummary) {
if (dgvCell.Value != null && !(dgvCe ll.Value is DBNull))
{
if (IsInteger(dgvCell.Value))
{
sumBox.Tag = Convert.ToInt64 (sumBox.Tag) + Convert.ToInt64(dgvCell.Value);
}
else if (IsDecimal(dgvCell.Valu e))
{
sumBox.Tag = Convert.ToDecim al(sumBox.Tag) + Convert.ToDecimal(dgvCell.Value);
}
sumBox.Text = string.Format("{0} ", sumBox.Tag);
sumBox.Invalidate();
}
}
}
}
}
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论