[csharp]
/// <summary>
/// DataGridView多行選擇求和
/// </summary>
/// <param name="myDataGridView">要求合計提示的DataGridView控件</param>
/// <param name="TooLar">傳入的ToolTip控件.</param>
/// <returns></returns>
public bool DataGridViewSum(DataGridView myDataGridView, ToolTip TooLar)
{
if (myDataGridView.SelectedCells.Count < 2)
{
TooLar.RemoveAll();
return false;
}
//如果選擇的單元格是全部,且大於:10000,則認為是不是求合計的操作
if (myDataGridView.SelectedCells.Count > 10000 && myDataGridView.SelectedCells.Count == myDataGridView.GetCellCount(DataGridViewElementStates.None))
{
TooLar.RemoveAll();
return false;
}
DataSet DS = new DataSet();
DataTable DtGroup = new DataTable("DtGroup");//保存列名
System.Collections.ArrayList NameMaxLength = new System.Collections.ArrayList();//保存DT中列名的長度
System.Collections.ArrayList ValueMaxLength = new System.Collections.ArrayList();//保存DT中列名的長度
DataTable DT = new DataTable("DT");//保存列名及數據
DtGroup.Columns.Add("ColumnName", System.Type.GetType("System.String"));//字段名
DT.Columns.Add("ColumnName", System.Type.GetType("System.String"));//字段名
DT.Columns.Add("ColumnValue", System.Type.GetType("System.Decimal"));//字段值
foreach (DataGridViewCell DC in myDataGridView.SelectedCells)
{
if (DC.ValueType.Name.ToLower() == "decimal" || DC.ValueType.Name.ToLower() == "int32")
{
DataRow DR;
DR = DT.NewRow();
DR["ColumnName"] = myDataGridView.Columns[DC.ColumnIndex].Name;
if (DC.Value == null)
{
DR["ColumnValue"] = 0;
}
else
{
DR["ColumnValue"] = DC.Value;
ValueMaxLength.Add(DC.Value.ToString().Length);
}
DT.Rows.Add(DR);
//在現有DtColName中查找是否包含了這個列名
if (DtGroup.Select("ColumnName='" + myDataGridView.Columns[DC.ColumnIndex].Name + "'").Length == 0)
{
DataRow DR1;
DR1 = DtGroup.NewRow();
DR1["ColumnName"] = myDataGridView.Columns[DC.ColumnIndex].Name;
DtGroup.Rows.Add(DR1);
NameMaxLength.Add(myDataGridView.Columns[DC.ColumnIndex].Name.Length);
}
}
}
DS.Tables.Add(DT);
DS.Tables.Add(DtGroup);
DataRelation dRelation = new DataRelation("dRelation", DS.Tables["DtGroup"].Columns["ColumnName"], DS.Tables["DT"].Columns["ColumnName"]);
DS.Relations.Add(dRelation);
DtGroup.Columns.Add("ColumnSum").Expression = "sum(child(dRelation).ColumnValue)";
string decSum = DT.Compute("sum(ColumnValue)", "").ToString();
NameMaxLength.Add("總合計:".Length);
ValueMaxLength.Add(decSum.Length);
DtGroup.AcceptChanges();
string Tall = "";
NameMaxLength.Sort();
ValueMaxLength.Sort();
int NameMaxLen = 0;
NameMaxLen = int.Parse(NameMaxLength[NameMaxLength.Count - 1].ToString());
int ValueMaxLen = 0;
ValueMaxLen = int.Parse(ValueMaxLength[ValueMaxLength.Count - 1].ToString());
for (int i = 0; i < DtGroup.Rows.Count; i++)
{
string strOneLine = "";
strOneLine = (DtGroup.Rows[i]["ColumnName"].ToString() + ":").PadRight(NameMaxLen + 1, ' ');
strOneLine = strOneLine + DtGroup.Rows[i]["ColumnSum"].ToString().PadLeft(ValueMaxLen, ' ');
Tall = Tall + strOneLine + System.Environment.NewLine;
}
Tall = Tall + "總合計:".PadRight(NameMaxLen + 1, ' ') + decSum.PadLeft(ValueMaxLen, ' ');
myDataGridView.ShowCellToolTips = false;
TooLar.ToolTipIcon = ToolTipIcon.Info;
TooLar.IsBalloon = true;//汽球形狀
TooLar.ToolTipTitle = "鼠標選定合計信息:";
TooLar.UseAnimation = true;//使用動畫效果
TooLar.UseFading = true;//淡入淡出效果
TooLar.BackColor = Color.Wheat;//工具提示的背景色
TooLar.AutoPopDelay = 10000;//顯示時間
TooLar.RemoveAll();
TooLar.SetToolTip(myDataGridView, Tall);
DT.Dispose();
DtGroup.Dispose();
DS.Dispose();
GC.Collect();
return true;
}