A Method for adding bottom aggregate rows to the datagridview [reprint]

Source: Internet
Author: User

 

The datagridview datagriuses many controls, but does not have the aggregate row function at the bottom. It is used in many places during a small project. The following method is used to implement this function:

Set the following

# Region: four methods for adding the bottom row Union count of the datagridview
And
# Endregion: four methods for adding the bottom row count of the datagridview
To your program.

Then, bind the following method and data table to the datagridview object (which is assumed to be datagridview1) to calculate the total:

It is assumed that the binding is performed after the button is clicked.

Private void button#click (Object sender, eventargs E)
{

Maid + = new eventhandler (maid );
Datagridview1.columnheadermouseclick + = new datagridviewcellmouseeventhandler (datagridview_columnheadermouseclick );
Maid + = new maid (maid );
Datagridview1.allowusertoaddrows = false;
Datagridview1.datasource = the data table to be bound;

}

 

# Region: four methods for adding the bottom row Union count of the datagridview
/// <Summary>
/// Computation and Computation
/// </Summary>
/// <Param name = "dgv"> the datagridview to be calculated </param>
Private void sumdatagridview (datagridview dgv)
{

# Region count
// Datagridview dgv = (datagridview) sender;
If (dgv. datasource = NULL) return;
Datatable dt = (datatable) dgv. datasource;
If (Dt. Rows. Count <1) return;
Decimal [] tal = new decimal [DT. Columns. Count];

Datarow NDR = DT. newrow ();

String talc = "";

Int number = 1;
Foreach (datarow DR in DT. Rows)
{
Dr ["@ Xu. Hao"] = Number ++;
Int n = 0;
Foreach (datacolumn DC in DT. columns)
{

If (talc = "" & DC. datatype. Name. toupper (). indexof ("string")> = 0) talc = Dc. columnname;

If (DC. datatype. isvaluetype)
{
String hej = Dr [DC. columnname]. tostring ();
Try
{
If (hej! = String. Empty) tal [N] + = decimal. parse (hej );
}
Catch (exception ){}
// If (hej! = String. Empty) tal [N] + = decimal. parse (hej );
}

N ++;
}
}

NDR. beginedit ();
For (INT I = 0; I <DT. Columns. Count; I ++)
{
If (TAL [I]! = 0)
NDR [I] = tal [I];
}
NDR ["@ Xu. Hao"] = (INT) (Dt. Rows. Count + 1). tostring ();
If (talc! = "") NDR [talc] = "[total]";
NDR. endedit ();
DT. Rows. Add (NDR );

Dgv. Rows [dgv. Rows. Count-1]. defaultcellstyle. backcolor = color. fromargb (255,255,210 );

If (dgv. Tag = NULL)
{
Foreach (datagridviewcolumn dgvc in dgv. columns)
{
Dgvc. sortmode = maid. Programmatic;
}
}

Dgv. Tag = NDR;

# Endregion

}
Private void datagridview_columnheadermouseclick (Object sender, datagridviewcellmouseeventargs E)
{//
// Directly copy the code

Datagridview sortdgv = (datagridview) sender;
Int FX = 0;
If (sortdgv. accessibledescription = NULL)
{
FX = 1;
}
Else
{
FX = int. parse (sortdgv. accessibledescription );
FX = (FX = 0? 1: 0 );
}
Sortdgv. accessibledescription = FX. tostring ();
If (sortdgv. Columns [E. columnindex]. Name = "@ Xu. Hao") return;
Datagridviewcolumn ncol = sortdgv. Columns [E. columnindex];

If (ncol. datapropertyname = string. Empty) return;

If (ncol! = NULL)
{
Sortdgv. Sort (ncol, FX = 0? Listsortdirection. ascending: listsortdirection. Descending );

}
//--
Datarow DR = (datarow) sortdgv. Tag;
Datatable dt = (datatable) sortdgv. datasource;
Datarow NDR = DT. newrow ();
NDR. beginedit ();
For (INT I = 0; I <DT. Columns. Count; I ++)
{
NDR [I] = Dr [I];
}
DT. Rows. Remove (DR );

// If (E. columnindex! = 0)
{
Int n = 1;
For (INT I = 0; I <sortdgv. Rows. Count; I ++)
{
Datagridviewrow dgrow = sortdgv. Rows [I];
Datarowview DRV = (datarowview) dgrow. databounditem;
Datarow TDR = DRV. row;
TDR. beginedit ();
TDR ["@ Xu. Hao"] = N;
N ++;
TDR. endedit ();

}
Sortdgv. Refresh ();
Sortdgv. refreshedit ();

}
NDR ["@ Xu. Hao"] = (INT) (Dt. Rows. Count + 1). tostring ();
NDR. endedit ();
DT. Rows. Add (NDR );
Sortdgv. Tag = NDR;

//--
Sortdgv. Sort (sortdgv. Columns ["@ Xu. Hao"], listsortdirection. ascending );
Sortdgv. Columns ["@ Xu. Hao"]. headercell. sortglyphdirection = sortorder. None;
Ncol. headercell. sortglyphdirection = FX = 0? Sortorder. ascending: sortorder. Descending;
Sortdgv. Rows [sortdgv. Rows. Count-1]. defaultcellstyle. backcolor = color. fromargb( 255,255,210 );

}
Private void datagridview_performancechanged (Object sender, eventargs E)
{
Datagridview dgv = (datagridview) sender;
Datatable dt = (datatable) dgv. datasource;
If (Dt = NULL) return;
Decimal [] tal = new decimal [DT. Columns. Count];
If (Dt. Columns. indexof ("@ Xu. Hao") <0)
{
Datacolumn Dc = new datacolumn ("@ Xu. Hao", system. type. GetType ("system. int32 "));
DT. Columns. Add (DC );
Dgv. Columns ["@ Xu. Hao"]. displayindex = 0;
Dgv. Columns ["@ Xu. Hao"]. headertext = "#";

Dgv. Columns ["@ Xu. Hao"]. sortmode = maid. Programmatic;
Dgv. autoresizecolumn (dgv. Columns ["@ Xu. Hao"]. Index );

Dgv. Columns ["@ Xu. Hao"]. defaultcellstyle. Alignment = maid. middlecenter;
}
Sumdatagridview (dgv );
}
Private void datagridview_cellvaluechanged (Object sender, datagridviewcelleventargs E)
{//
Datagridview dgv = (datagridview) sender;
If (dgv. Tag = NULL | E. rowindex <0 | E. rowindex = dgv. Rows. Count-1) return;

String Col = dgv. Columns [E. columnindex]. datapropertyname;
If (COL = string. Empty) return;
If (datarowview) dgv. Rows [E. rowindex]. databounditem). Row. Table. Columns [col]. datatype. isvaluetype)
{
Decimal tal = 0;
Foreach (maid in dgv. Rows)
{
If (dgvr. Index! = Dgv. Rows. Count-1)
{
String hej = dgvr. cells [E. columnindex]. value. tostring ();
If (hej! = String. Empty) tal + = decimal. parse (hej );
}
}
If (TAL = 0)
Dgv [E. columnindex, dgv. Rows. Count-1]. value = dbnull. value;
Else
Dgv [E. columnindex, dgv. Rows. Count-1]. value = Tal;
}
}
# Endregion: four methods for adding the bottom row count of the datagridview

 

I have run it in multiple places in the project, but I don't know how universal it is ?!
Welcome!

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.