Implement the aggregation function of specified columns in the DataGrid in ASP. NET, and support template column Aggregation

Source: Internet
Author: User

/// <Summary>
/// Vertically merge the same text cells in the specified Column
/// </Summary>
/// <Usage>
/// Mergesamecellattextcolumn (datagridinstance, columnindex );
/// </Usage>
/// <Param name = "curgrid"> </param>
/// <Param name = "columnindex"> </param>
Private void mergesamecellattextcolumn (DataGrid curgrid, int columnindex)
{
Datagriditem curitem = NULL;
Datagriditem nextitem = NULL;
For (INT r = 0; r <curgrid. Items. Count; r ++)
{
/// Current cell
Curitem = curgrid. items [R];
/// Cell under it
If (R + 1 <curgrid. Items. Count)
Nextitem = curgrid. items [R + 1];
Else
Nextitem = NULL;
String curvalue = curitem = NULL? String. Empty: curitem. cells [columnindex]. text;
String nextvalue = nextitem = NULL? String. Empty: nextitem. cells [columnindex]. text;
/// If the cell content is the same
If (curvalue! = String. Empty & nextvalue! = String. Empty & curvalue = nextvalue)
{

/// Set the cells under it to invisible
If (nextvalue! = NULL) nextitem. cells [columnindex]. Visible = false;
/// If the current cell is visible, set its rowspan + 1
If (curitem. cells [columnindex]. Visible = true)
{
Curitem. cells [columnindex]. rowspan + = 1;
}
/// If the current cell is invisible, it is directly traced back to its first visible cell.
Else if (curitem. cells [columnindex]. Visible = false)
{
Int prerowindex = R;
While (prerowindex> = 0 & curgrid. items [prerowindex]. cells [columnindex]. Visible = false)
{
Prerowindex-= 1;
}
/// Set rowspan + 1 of the first visible cell
Curgrid. items [prerowindex]. cells [columnindex]. rowspan + = 1;
}
}
/// If the cell content is different
Else if (curvalue! = String. Empty & nextvalue! = String. Empty & curvalue! = Nextvalue)
{
/// Determine whether the row is a critical row: The preceding row or multiple rows have different content from the corresponding cells of the following row or multiple rows.
If (curitem. cells [columnindex]. Visible = false)
{
Int prerowindex = R;
While (prerowindex> = 0 & curgrid. items [prerowindex]. cells [columnindex]. Visible = false)
{
Prerowindex-= 1;
}
/// Set rowspan + 1 of the first visible cell
Curgrid. items [prerowindex]. cells [columnindex]. rowspan + = 1;
}
Else
{
/// Non-critical
}
}
/// The last row of the Data list
Else if (r = curgrid. Items. Count-1 & curitem. cells [columnindex]. Visible = false)
{
Int prerowindex = R;
While (prerowindex> = 0 & curgrid. items [prerowindex]. cells [columnindex]. Visible = false)
{
Prerowindex-= 1;
}
/// Set rowspan + 1 of the first visible cell
Curgrid. items [prerowindex]. cells [columnindex]. rowspan + = 1;
}
}

}
/// <Summary>
/// Vertically merge the same template cell of the specified Column
/// </Summary>
/// <Usage>
/// Mergesamecellattemplatecolumn (maid, "templatecontrolid", columnindex );
/// </Usage>
/// <Param name = "curgrid"> </param>
/// <Param name = "columnindex"> </param>
/// <Param name = "controlname"> </param>
Private void mergesamecellattemplatecolumn (DataGrid curgrid, int columnindex, string controlname)
{
Datagriditem curitem = NULL;
Datagriditem nextitem = NULL;
For (INT r = 0; r <curgrid. Items. Count; r ++)
{
/// Current cell
Curitem = curgrid. items [R];
/// Cell under it
If (R + 1 <curgrid. Items. Count)
Nextitem = curgrid. items [R + 1];
Else
Nextitem = NULL;
/// When calling, pay attention to modifying the template control type and the method for obtaining the template control value.
String curvalue = curitem = NULL? String. Empty :( (ListBox) curitem. findcontrol (controlname). selectedtext;
String nextvalue = nextitem = NULL? String. Empty :( (ListBox) nextitem. findcontrol (controlname). selectedtext;
/// If the cell content is the same
If (curvalue! = String. Empty & nextvalue! = String. Empty & curvalue = nextvalue)
{
/// Set the cells under it to invisible
If (nextvalue! = NULL) nextitem. cells [columnindex]. Visible = false;
/// If the current cell is visible, set its rowspan + 1
If (curitem. cells [columnindex]. Visible = true)
{
Curitem. cells [columnindex]. rowspan + = 1;
}
/// If the current cell is invisible, it is directly traced back to its first visible cell.
Else if (curitem. cells [columnindex]. Visible = false)
{
Int prerowindex = R;
While (prerowindex> = 0 & curgrid. items [prerowindex]. cells [columnindex]. Visible = false)
{
Prerowindex-= 1;
}
/// Set rowspan + 1 of the first visible cell
Curgrid. items [prerowindex]. cells [columnindex]. rowspan + = 1;
}
}
/// If the cell content is different
Else if (curvalue! = String. Empty & nextvalue! = String. Empty & curvalue! = Nextvalue)
{
/// Determine whether the row is a critical row: The preceding row or multiple rows have different content from the corresponding cells of the following row or multiple rows.
If (curitem. cells [columnindex]. Visible = false)
{
Int prerowindex = R;
While (prerowindex> = 0 & curgrid. items [prerowindex]. cells [columnindex]. Visible = false)
{
Prerowindex-= 1;
}
/// Set rowspan + 1 of the first visible cell
Curgrid. items [prerowindex]. cells [columnindex]. rowspan + = 1;
}
Else
{
/// Non-critical
}
}
/// The last row of the Data list
Else if (r = curgrid. Items. Count-1 & curitem. cells [columnindex]. Visible = false)
{
Int prerowindex = R;
While (prerowindex> = 0 & curgrid. items [prerowindex]. cells [columnindex]. Visible = false)
{
Prerowindex-= 1;
}
/// Set rowspan + 1 of the first visible cell
Curgrid. items [prerowindex]. cells [columnindex]. rowspan + = 1;
}
}

}

 

Related Article

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.