这几天做一个项目有用到表格显示数据的地方,客户要求重复的数据列需要合并,就总结了一下GridView 和 Repeater 关于重复数据合并的方法。
效果图如下 :
GridView :
前台代码 :
'>'>'>'>
GridView 前台代码后台代码 :
DataBind() 2 { ; 4 SqlDataAdapter sda = new SqlDataAdapter(sql, cn); 5 DataSet ds = new DataSet(); 6 sda.Fill(ds); 7 gvIncome.DataSource = ds; 8 gvIncome.DataBind(); colnum = gvIncome.Columns.Count; MergeRows(gvIncome, ); } MergeRows(GridView gvw, int colnum, string controlNameo) 14 { { ( { GridViewRow previousRow = gvw.Rows[rowIndex + Label row_lbl = row.Cells[col].FindControl(controlName) Label previousRow_lbl = previousRow.Cells[col].FindControl(controlName) (row_lbl != { { row.Cells[col].RowSpan = previousRow.Cells[col].RowSpan < 1 ? 2 : previousRow.Cells[col].RowSpan + 1; previousRow.Cells[col].Visible = false; 32 } 33 } 34 } 35 } 36 37 }
GridView 后台代码
********************************************************
*
华丽的分割线 *
********************************************************
Repeater :
前台代码 :
1 // table样式 //***************** 一级二级三级四级
Repeater 前台代码
后台代码 :
DataBind() 2 { ; 4 SqlDataAdapter sda = new SqlDataAdapter(sql, cn); 5 DataSet ds = new DataSet(); 6 sda.Fill(ds); 7 rptIncome.DataSource = ds; 8 rptIncome.DataBind(); ( { ; 13 string tdIdName1 = rpttd + i.ToString(); } 16 17 } MergeCell(string tdIdName1) 24 { { 27 MergeCellSet(tdIdName1, i); 28 } 29 } MergeCellSet(string tdIdName1, int i) 36 { HtmlTableCell cell = rptIncome.Items[i].FindControl(tdIdName1) cell.RowSpan = (cell.RowSpan == -cellPrev.RowSpan = (cellPrev.RowSpan == -(cell.InnerText == cellPrev.InnerText) 42 { cellPrev.RowSpan += cell.RowSpan; 45 cell.Visible = false; // 隐藏当前行 } 49 }
Repeater 后台代码