1.
<asp:TemplateField HeaderText="总额"> <ItemTemplate> <asp:Label ID="lblqty" runat="server" Text='<%#Bind("QTY") %>'>'></asp:Label>//绑定数据源字段 </ItemTemplate> </asp:TemplateField>
2.合并某一列相同的行
/// <summary> /// 合并GridView列中相同的行 /// </summary> /// <param name="GridView1">GridView对象</param> /// <param name="cellNum">需要合并的列</param> public static void GroupRows(GridView GridView1, int cellNum) { int i = 0, rowSpanNum = 1; while (i < GridView1.Rows.Count - 1) { GridViewRow gvr = GridView1.Rows[i]; for (++i; i < GridView1.Rows.Count; i++) { GridViewRow gvrNext = GridView1.Rows[i]; if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].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; } } } }
2.2根据某一列合并相同的行 同时合并其他指定列
/// <summary> /// 合并GridView列中相同的行 /// 根据GridView某特定相同的行 同时可以合并其他列 /// </summary> /// <param name="GridView1">GridView对象</param> /// <param name="cellNum">需要合并的列</param> public static void GroupRows(GridView GridView1, int cellNum) { int i = 0, rowSpanNum = 1; while (i < GridView1.Rows.Count - 1) { GridViewRow gvr = GridView1.Rows[i];//上一行 for (++i; i < GridView1.Rows.Count; i++) { GridViewRow gvrNext = GridView1.Rows[i];//下一行 if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)//判断合并的条件 { gvrNext.Cells[cellNum].Visible = false; gvrNext.Cells[cellNum + 1].Visible = false; gvrNext.Cells[cellNum + 4].Visible = false; rowSpanNum++; } else { gvr.Cells[cellNum].RowSpan = rowSpanNum; gvr.Cells[cellNum + 1].RowSpan = rowSpanNum; gvr.Cells[cellNum + 4].RowSpan = rowSpanNum; rowSpanNum = 1; break; } if (i == GridView1.Rows.Count - 1) { gvr.Cells[cellNum].RowSpan = rowSpanNum; gvr.Cells[cellNum + 1].RowSpan = rowSpanNum; gvr.Cells[cellNum + 4].RowSpan = rowSpanNum; } } } }