(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.