Merge the same columns of the GridView and DataGrid

Source: Internet
Author: User

(1) Common Columns

/// <Summary> /// merge the Gridview columns (Common columns, excluding template columns) /// Note: 1. the same rows can be put together by grouping and sorting when the GridView is bound. // 2. method Application Time, you should use /// </summary> /// <param name = "gv"> the Gridview object to be merged </param> /// <param name = "columnIndex"> index of the column to be merged </param> public static void UnitCell (GridView gv, int columnIndex) {int I = 0; // The current row string lastType = string. empty; // determines whether to merge the value of the corresponding column of the row int lastCell = 0; // determines the index of the last row with the same value if (gv. rows. count> 0) {lastType = gv. rows [0]. cells [columnIndex]. text. toString (); gv. rows [0]. cells [columnIndex]. rowSpan = 1; lastCell = 0;} for (I = 1; I <gv. rows. count; I ++) {if (gv. rows [I]. cells [columnIndex]. text = lastType) {gv. rows [I]. cells [columnIndex]. visible = false; gv. rows [lastCell]. cells [columnIndex]. rowSpan ++;} else {lastType = gv. rows [I]. cells [columnIndex]. text. toString (); lastCell = I; gv. rows [I]. cells [columnIndex]. rowSpan = 1 ;}}/// <summary> // merge DataGrid columns (Common columns, excluding template columns) /// Note: 1. when the DataGrid is bound, it can be grouped and sorted so that the same rows can be put together. // 2. method Application Time, you should use the /// </summary> /// <param name = "dg"> DataGrid object to be merged </param> /// <param name = "columnIndex"> index of the column to be merged </param> public static void UnitCell_T (DataGrid dg, int columnIndex) {int I = 0; // The current row string lastType = string. empty; // determines whether to merge the value of the corresponding column of the row int lastCell = 0; // determines the index of the last row with the same value if (dg. items. count> 0) {lastType = dg. items [0]. cells [columnIndex]. text. toString (); dg. items [0]. cells [columnIndex]. rowSpan = 1; lastCell = 0;} for (I = 1; I <dg. items. count; I ++) {if (dg. items [I]. cells [columnIndex]. text = lastType) {dg. items [I]. cells [columnIndex]. visible = false; dg. items [lastCell]. cells [columnIndex]. rowSpan ++;} else {lastType = dg. items [I]. cells [columnIndex]. text. toString (); lastCell = I; dg. items [I]. cells [columnIndex]. rowSpan = 1 ;}}}

(2) template Columns

/// <Summary> // merge the Gridview columns (template columns) /// </summary> /// <param name = "gv"> the GridView object to be merged </param> /// <param name = "columnIndex"> to be merged column index </param> /// <param name = "lblName"> ID of the template column object </param> public static void UnitCell (GridView gv, int columnIndex, string lblName) {int I = 0; // The current row string lastType = string. empty; // determines whether to merge the value of the corresponding column of the row int lastCell = 0; // determines the index of the last row with the same value if (gv. rows. count> 0) {lastType = (gv. rows [0]. cells [columnIndex]. findControl (lblName) as Label ). text; gv. rows [0]. cells [columnIndex]. rowSpan = 1; lastCell = 0;} for (I = 1; I <gv. rows. count; I ++) {if (gv. rows [I]. cells [columnIndex]. findControl (lblName) as Label ). text = lastType) {gv. rows [I]. cells [columnIndex]. visible = false; gv. rows [lastCell]. cells [columnIndex]. rowSpan ++;} else {lastType = (gv. rows [I]. cells [columnIndex]. findControl (lblName) as Label ). text. toString (); lastCell = I; gv. rows [I]. cells [columnIndex]. rowSpan = 1 ;}}}

(3) Call It In The DataBound event.

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.