protected void gridView_PreRender(object sender, EventArgs e)
{
MergeRows(gridView);
}
//合並單元格
public static void MergeRows(GridView gridView)
{
for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)//这里不动
{
GridViewRow row = gridView.Rows[rowIndex];
GridViewRow previousRow = gridView.Rows[rowIndex + 1];
//遍历每一行的每一列(把需要的列进行合并)
for (int i = 0; i < 9; i++)//i<row.Cells.Count 這裡只需將前8列合并单元格,故i < 9
{
if (row.Cells[i].Text == previousRow.Cells[i].Text)//行合併 RowSpan
{
row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : previousRow.Cells[i].RowSpan + 1;
previousRow.Cells[i].Visible = false;
}
}
//补充,合并“合计”//列合并
if (previousRow.Cells[7].Text == "合計")
{
previousRow.Cells[7].ColumnSpan = 2;
previousRow.Cells[8].Visible = false;
}
}
}
//前端代码
<asp:GridView runat="server" ID="gridView" AutoGenerateColumns="False" OnPreRender="gridView_PreRender">
<Columns>
<asp:BoundField DataField="SITE" HeaderText="廠區" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="LIST_NO" HeaderText="列支單號" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="ISSUE_LIST_DATE" HeaderText="出名單日期" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="EXPENSE_DATE" HeaderText="預估列支日期" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="REMARK" HeaderText="費用說明" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="COST_TYPE" HeaderText="費用項目" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="RULE_MODEL" HeaderText="規則模式" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="GROUP_MAIN_NAME" HeaderText="事業群法人" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="DISPATCHING_COMPANY" HeaderText="派遣公司" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="ACTUAL_EMP_CNT" HeaderText="出名單人數" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="ACTUAL_AMOUNT" HeaderText="出名單金額" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="WRITE_OFF_AMOUNT" HeaderText="沖賬金額" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="TOTAL_AMOUNT" HeaderText="列支金額" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="CHANNEL_AMOUNT" HeaderText="派遣公司金額" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="AGENT_AMOUNT" HeaderText="代理金額" ReadOnly="True"></asp:BoundField>
</Columns>
</asp:GridView>
效果如下:
列1 | 列2 | 列3 | 列4 | 列5 | 列6 | 列7 | 列8 | 列9 | 列10 | 列11 | 列12 | 列13 | 列14 |
A | 甲 | 1 | 11 | one | 甲 | 甲 | A | A1 | 1 | 1 | 1 | 1 | 1 |
A2 | 1 | 1 | 1 | 1 | 1 | ||||||||
小计 | 2 | 2 | 2 | 2 | 2 | ||||||||
B | B1 | 1 | 1 | 1 | 1 | 1 | |||||||
小计 | 1 | 1 | 1 | 1 | 1 | ||||||||
合计 | 3 | 3 | 3 | 3 | 3 |