ASP.NET中GridView 重復(fù)表格列合并的實(shí)現(xiàn)方法
這幾天做一個項(xiàng)目有用到表格顯示數(shù)據(jù)的地方,客戶要求重復(fù)的數(shù)據(jù)列需要合并,就總結(jié)了一下GridView 和 Repeater 關(guān)于重復(fù)數(shù)據(jù)合并的方法。

效果圖如下:

GridView :
前臺代碼 :
<div>
<asp:GridView ID="gvIncome" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="一級">
<ItemTemplate>
<asp:Label ID="Label0" runat="server" Text='<%#Eval("aname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="二級">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("bname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="三級">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("cname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="四級">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("dname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
GridView 前臺代碼
<span style="line-height: 1.5; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; background-color: rgb(255, 255, 255);"> </span>
后臺代碼 :
public void DataBind()
{
string sql = "select a.aname,b.bname,c.cname ,d.dname from aa as a right join bb as b on a.aid=b.aid right join cc as c on b.bid=c.bid left join dd as d on d.cid=c.cid order by a.aid";
SqlDataAdapter sda = new SqlDataAdapter(sql, cn);
DataSet ds = new DataSet();
sda.Fill(ds);
gvIncome.DataSource = ds;
gvIncome.DataBind();
//MergeRows(gvIncome.HeaderRow, gvIncome.Rows.Count);
int colnum = gvIncome.Columns.Count; // 獲取GridView中獲取列數(shù)
MergeRows(gvIncome, 4, "Label"); // GridView 要整合的列數(shù) 需要改變的Lable控件
}
public static void MergeRows(GridView gvw, int colnum, string controlNameo)
{
for (int col = 0; col < colnum; col++) // 遍歷每一列
{
string controlName = controlNameo + col.ToString(); // 獲取當(dāng)前列需要改變的Lable控件ID
for (int rowIndex = gvw.Rows.Count - 2; rowIndex >= 0; rowIndex--) //GridView中獲取行數(shù) 并遍歷每一行
{
GridViewRow row = gvw.Rows[rowIndex]; // 獲取當(dāng)前行
GridViewRow previousRow = gvw.Rows[rowIndex + 1]; // 獲取當(dāng)前行 的上一行
Label row_lbl = row.Cells[col].FindControl(controlName) as Label; //// 獲取當(dāng)前列當(dāng)前行 的 Lable 控件ID 的文本
Label previousRow_lbl = previousRow.Cells[col].FindControl(controlName) as Label; //// 獲取當(dāng)前列當(dāng)前行 的上一行 的 Lable控件ID 的文本
if (row_lbl != null && previousRow_lbl != null) // 如果當(dāng)前行 和 上一行 要改動的 Lable 的ID 的文本不為空
{
if (row_lbl.Text == previousRow_lbl.Text) // 如果當(dāng)前行 和 上一行 要改動的 Lable 的ID 的文本不為空 且相同
{
// 當(dāng)前行的當(dāng)前單元格(單元格跨越的行數(shù)。 默認(rèn)值為 0 ) 與下一行的當(dāng)前單元格的跨越行數(shù)相等且小于一 則 返回2 否則讓上一行行的當(dāng)前單元格的跨越行數(shù)+1
row.Cells[col].RowSpan = previousRow.Cells[col].RowSpan < 1 ? 2 : previousRow.Cells[col].RowSpan + 1;
//并讓上一行的當(dāng)前單元格不顯示
previousRow.Cells[col].Visible = false;
}
}
}
}
}
GridView 后臺代碼
Repeater :
前臺代碼 :
// table樣式
<style>
table {
border-collapse:collapse;
}
table tr td,th {
border:1px solid black;
}
</style>
//*****************
<div>
<table>
<tr>
<th>一級</th> <th>二級</th> <th>三級</th> <th>四級</th>
</tr>
<asp:Repeater ID="rptIncome" runat="server">
<ItemTemplate>
<tr>
<td runat="server" id="td0"><%#Eval("aname") %></td>
<td runat="server" id="td1"><%#Eval("bname") %></td>
<td runat="server" id="td2"><%#Eval("cname") %></td>
<td runat="server" id="td3"><%#Eval("dname") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
Repeater 前臺代碼
后臺代碼 :
public void DataBind()
{
string sql = "select a.aname,b.bname,c.cname ,d.dname from aa as a right join bb as b on a.aid=b.aid right join cc as c on b.bid=c.bid left join dd as d on d.cid=c.cid order by a.aid";
SqlDataAdapter sda = new SqlDataAdapter(sql, cn);
DataSet ds = new DataSet();
sda.Fill(ds);
rptIncome.DataSource = ds;
rptIncome.DataBind();
for (int i = 0; i < 4; i++) // 遍歷每一列
{
string rpttd = "td";
string tdIdName1 = rpttd + i.ToString();
MergeCell(tdIdName1); // 把當(dāng)前列的 td 的 ID文本作為方法的參數(shù)
}
}
/// <summary>
///
/// </summary>
/// <param name="tdIdName1">當(dāng)前列當(dāng)前行的 td 的ID文本</param>
private void MergeCell(string tdIdName1)
{
for (int i = rptIncome.Items.Count - 1; i > 0; i--) // rptIncome.Items.Count - 1 數(shù)據(jù)總行數(shù)(數(shù)據(jù)從0開始) 遍歷當(dāng)前列的每一行
{
MergeCellSet(tdIdName1, i);
}
}
/// <summary>
///
/// </summary>
/// <param name="tdIdName1">當(dāng)前列當(dāng)前行的 td 的ID文本</param>
/// <param name="i">當(dāng)前行</param>
private void MergeCellSet(string tdIdName1, int i)
{
HtmlTableCell cellPrev = rptIncome.Items[i - 1].FindControl(tdIdName1) as HtmlTableCell; // 獲取下一行當(dāng)前列的 td 所在的單元格
HtmlTableCell cell = rptIncome.Items[i].FindControl(tdIdName1) as HtmlTableCell; // 獲取當(dāng)前行當(dāng)前列的 td 所在的單元格
cell.RowSpan = (cell.RowSpan == -1) ? 1 : cell.RowSpan; // 獲取當(dāng)前行當(dāng)前列單元格跨越的行數(shù)
cellPrev.RowSpan = (cellPrev.RowSpan == -1) ? 1 : cellPrev.RowSpan; // 獲取下一行當(dāng)前列單元格跨越的行數(shù)
if (cell.InnerText == cellPrev.InnerText)
{
// 讓下一行的當(dāng)前單元格的跨越行數(shù) + 當(dāng)前行的跨越行數(shù)
cellPrev.RowSpan += cell.RowSpan;
cell.Visible = false; // 隱藏當(dāng)前行
//關(guān)鍵代碼,再判斷執(zhí)行第2列的合并單元格方法
}
}
Repeater 后臺代碼
以上所述是小編給大家介紹的ASP.NET中GridView 重復(fù)表格列合并的實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- asp.net實(shí)現(xiàn)固定GridView標(biāo)題欄的方法(凍結(jié)列功能)
- 在ASP.NET 2.0中操作數(shù)據(jù)之六十四:GridView批量添加數(shù)據(jù)
- 在ASP.NET 2.0中操作數(shù)據(jù)之十:使用 GridView和DetailView實(shí)現(xiàn)的主/從報(bào)表
- ASP.NET數(shù)據(jù)綁定GridView控件使用技巧
- ASP.NET數(shù)據(jù)綁定之GridView控件
- ASP.NET中GridView的文件輸出流方式
- ASP.NET中GridView、DataList、DataGrid三個數(shù)據(jù)控件foreach遍歷用法示例
- asp.net GridView中使用RadioButton單選按鈕的方法
- asp.net Checbox在GridView中的應(yīng)用實(shí)例分析
相關(guān)文章
asp.net的web頁面(aspx)數(shù)據(jù)量過多時提交失敗對策
asp.net的web頁面,數(shù)據(jù)量過多時提交失敗的情況想必有很多朋友都有遇到過吧,下面與大家分享下詳細(xì)的解決方法2013-05-05
ASP.NET技巧:請求網(wǎng)址并解析返回的html
ASP.NET技巧:請求網(wǎng)址并解析返回的html...2006-09-09
如何利用IIS調(diào)試ASP.NET網(wǎng)站程序詳解
這篇文章主要給大家介紹了關(guān)于如何利用IIS調(diào)試ASP.NET網(wǎng)站程序的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
.NET Core控制臺應(yīng)用程序如何使用異步(Async)Main方法詳解
這篇文章主要給大家介紹了關(guān)于.NET Core控制臺應(yīng)用程序如何使用異步(Async)Main方法的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
.Net8.0 WebApi發(fā)布到IIS詳細(xì)步驟
本文主要介紹了.Net8.0 WebApi發(fā)布到IIS詳細(xì)步驟, 文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
ASP.NET MVC 從IHttp到頁面輸出的實(shí)例代碼
MVCHandler應(yīng)該算是MVC真正開始的地方。MVCHandler實(shí)現(xiàn)了IHttpHandler接口,ProcessRequest便是方法入口2013-09-09
ASP.NET FileUpload 上傳圖片實(shí)例
Add a FileUpload control to the aspx page2009-09-09
如何使用簽名保證ASP.NET MVC OR WEBAPI的接口安全
這篇文章主要介紹了如何使用簽名保證ASP.NET MVC OR WEBAPI的接口安全,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-04-04
ASP.NET中畫圖形驗(yàn)證碼的實(shí)現(xiàn)代碼
這篇文章給大家介紹了asp.net中畫圖形驗(yàn)證碼的實(shí)現(xiàn)方法,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01
asp.net+ajaxfileupload.js 實(shí)現(xiàn)文件異步上傳代碼分享
本文給大家分享一段asp.net基于ajaxfileupload.js實(shí)現(xiàn)文件異步上傳的代碼,本人項(xiàng)目中已經(jīng)在使用的代碼,小伙伴們可以直接移植到自己的項(xiàng)目中去。2014-11-11

