When creating a project, you need to merge columns with the same information in the gridview. So google found the following example: Solution ): 1 /// <summary> 2 // merge rows with the same information in a column in the GridView (cell) 3 /// </summary> 4 /// <param name = "GridView1"> GridView </param> 5 /// <param name = "cellNum"> column number </param> 6 public static void GroupRows (GridView GridView1, int cellNum) 7 { 8 int I = 0, rowSpanNum = 1; 9 while (I <GridView1.Rows. Count-1) 10 { 11 GridViewRow gvr = GridView1.Rows [I]; 12 13 for (++ I; I <GridView1.Rows. Count; I ++) 14 { 15 GridViewRow gvrNext = GridView1.Rows [I]; 16 if (gvr. Cells [cellNum]. Text = gvrNext. Cells [cellNum]. Text) 17 { 18 gvrNext. Cells [cellNum]. Visible = false; 19 rowSpanNum ++; 20} 21 else 22 { 23 gvr. Cells [cellNum]. RowSpan = rowSpanNum; 24 rowSpanNum = 1; 25 break; 26} 27 28 if (I = GridView1.Rows. Count-1) 29 { 30 gvr. Cells [cellNum]. RowSpan = rowSpanNum; 31} 32} 33} 34} 35 36 But when I copy the code to my project, I cannot get the desired result, which is equivalentJinma storeSo I read the code again and found that there was no problem. After being depressed for a long time, I found that cells [cellNum] in the above example. the text value is the value in the row, and the merged value isTemplate ColumnLable in, modified: Int I = 0, rowSpanNum = 1; Int cellNum = 0 ;//Here, the cellnum value is the index of the column you want to merge, starting from 0. While (I <GridView1.Rows. Count-1) { GridViewRow gvr = GridView1.Rows [I];
Label tmplable = (Label) gvr. FindControl ("UpUnitName ");
For (++ I; I <GridView1.Rows. Count; I ++) { GridViewRow gvrNext = GridView1.Rows [I]; Label tmplable1 = (Label) gvrNext. FindControl ("UpUnitName "); If (tmplable. Text = tmplable1.Text) { GvrNext. Cells [cellNum]. Visible = false; RowSpanNum ++; } Else { Gvr. Cells [cellNum]. RowSpan = rowSpanNum; RowSpanNum = 1; Break; }
If (I = GridView1.Rows. Count-1) { Gvr. Cells [cellNum]. RowSpan = rowSpanNum; } }
|