DataGridView control add mouse selected ToolTip display total

Source: Internet
Author: User

[Csharp]
/// <Summary>
/// Select the sum of multiple rows in the DataGridView
/// </Summary>
/// <Param name = "myDataGridView"> the DataGridView control that requires the total display </param>
/// <Param name = "TooLar"> input ToolTip control. </param>
/// <Returns> </returns>
Public bool DataGridViewSum (DataGridView mydatagri, ToolTip TooLar)
{
If (myDataGridView. SelectedCells. Count <2)
{
TooLar. RemoveAll ();
Return false;
}
// If the selected cells are all and greater than: 10000, the total operation is not considered.
If (myDataGridView. SelectedCells. Count> 10000 & myDataGridView. SelectedCells. Count = myDataGridView. GetCellCount (maid. None ))
{
TooLar. RemoveAll ();
Return false;
}

DataSet DS = new DataSet ();
DataTable DtGroup = new DataTable ("DtGroup"); // Save the column name
System. Collections. ArrayList NameMaxLength = new System. Collections. ArrayList (); // Save the length of the column name in DT
System. Collections. ArrayList ValueMaxLength = new System. Collections. ArrayList (); // Save the length of the column name in DT
DataTable DT = new DataTable ("DT"); // Save the column name and Data
DtGroup. Columns. Add ("ColumnName", System. Type. GetType ("System. String"); // field name

DT. Columns. Add ("ColumnName", System. Type. GetType ("System. String"); // field name
DT. Columns. Add ("ColumnValue", System. Type. GetType ("System. Decimal"); // Field Value

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 );
// Check whether the column name is included in the existing 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 ("Total:". 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 + "Total:". PadRight (NameMaxLen + 1, '') + decSum. PadLeft (ValueMaxLen ,'');

MyDataGridView. ShowCellToolTips = false;
TooLar. ToolTipIcon = ToolTipIcon. Info;
TooLar. IsBalloon = true; // ball shape
TooLar. ToolTipTitle = "Mouse selected total information :";
TooLar. UseAnimation = true; // use the animation effect
TooLar. UseFading = true; // fade-in and fade-out effect
TooLar. BackColor = Color. Wheat; // specifies the background Color of the tooltip.
TooLar. AutoPopDelay = 10000; // display time
TooLar. RemoveAll ();
TooLar. SetToolTip (myDataGridView, Tall );
DT. Dispose ();
DtGroup. Dispose ();
DS. Dispose ();
GC. Collect ();
Return true;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.