Asp. The implementation method of the GridView repeating table column merging in net _ practical tips

Source: Internet
Author: User
Tags dname

These days to do a project useful to the table to display data where the customer requested a duplicate data column needs to be merged, summed up the GridView and Repeater about the method of data merge.

The effect chart is as follows:

Gridview:

Foreground code:

<div> <asp:gridview id= "gvincome" runat= "Server" autogeneratecolumns= "False" > <Columns> <asp: TemplateField headertext= "Level" > <ItemTemplate> <asp:label id= "Label0" runat= "server" text= ' <% #Eval (" Aname ")%> ' ></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:templatefield Headertext= "Level Two" > <ItemTemplate> <asp:label id= "Label1" runat= "server" text= ' <% #Eval ("bname")%> '
></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:templatefield headertext= "Level three" > <ItemTemplate> <asp:label id= "Label2" runat= "server" text= ' <% #Eval ("CNAME")%> ' ></asp:label > </ItemTemplate> </asp:TemplateField> <asp:templatefield headertext= "level four" > <itemtemplate > <asp:label id= "Label3" runat= "server" text= ' <% #Eval ("dname")%> ' ></asp:Label> </ itemtemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> GridView Front Code 
<span style= "line-height:1.5; Font-family:verdana, Arial, Helvetica, Sans-serif; font-size:14px; Background-color:rgb (255, 255, 255); > </span>

Background code:

public void DataBind () {String sql = ' Select A.aname,b.bname,c.cname, d.dname from AA to 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; Gets the number of columns fetched in the GridView Mergerows (Gvincome, 4, "Label"); The number of columns to be consolidated by the GridView need to be changed lable control} public static void Mergerows (GridView GVW, int colnum, string Controlnameo) {for (int c OL = 0; Col < Colnum; col++)//Traverse each column {string controlname = Controlnameo + col. ToString (); Gets the lable control ID for (int rowIndex = GVW) that the current column needs to change. Rows.count-2; RowIndex >= 0; rowindex--)//gridview Gets the number of rows and traverses each row {gridviewrow row = GVW. Rows[rowindex]; Gets the current row gridviewrow Previousrow = GVW. Rows[rowindex + 1]; Gets the top line Label row_lbl = row in the current row. Cells[col]. FindControl (controlname) as LaBel Gets the text Label PREVIOUSROW_LBL = Previousrow.cells[col] of the lable control ID for the current row of the current column. FindControl (controlname) as Label; Gets the text if (row_lbl!= null && previousrow_lbl!= null) of the previous row of the current column's lable control ID//If the ID of the lable to be changed on the current row and the previous line The text is not empty {if (ROW_LBL). Text = = Previousrow_lbl. Text)///If the current row and the ID of the lable you want to change are not empty and the same {//when the current cell in the row (the number of rows spanned by the cell). The default value of 0 is equal to the number of rows across the current cell in the next row and is less than one return of 2 otherwise, the number of rows in the current cell of the previous row is +1 row. Cells[col]. RowSpan = Previousrow.cells[col]. RowSpan < 1? 2:previousrow.cells[col].
RowSpan + 1; And let the current cell of the previous row not display Previousrow.cells[col].
Visible = false; The GridView background code in}}}}

Repeater:

Foreground code:

Table Style
<style>
table {
border-collapse:collapse;
}
Table TR td,th {
border:1px solid black;
}
</style>
//*****************
<div>
<table>
<tr>
<th> level </th> <th> Level Two </th> <th> level three </th> <th> four </th> </tr>
<asp : Repeater id= "Rptincome" runat= "Server" >
<ItemTemplate>
<tr>
<td runat= "Server" Id= "Td0" ><% #Eval ("Aname")%></td> <td "
server" runat= "id=" TD1 #Eval ("><%") bname </td>
<td runat= "Server" id= "TD2" ><% #Eval ("CNAME")%></td> <td
"Server" Id= "Td3" ><% #Eval ("dname")%></td>
</tr>
</ItemTemplate>
</asp: repeater>
</table>
</div>
Repeater Foreground code

Background code:

public void DataBind () {String sql = ' Select A.aname,b.bname,c.cname, d.dname from AA to 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++)//Traverse each column {string rpttd = "TD"; string tdIdName1 = Rpttd + i.tostring (); Mergecell (TDIDNAME1); The ID text of the current listed TD is used as the parameter of the method}///<summary>//////</summary>///<param name= "tdIdName1" > The current row of TD in the current column ID text </param> private void Mergecell (string tdIdName1) {for (int i = rptincome.items.count-1; i > 0; i--)//RP 
Tincome.items.count-1 the total number of rows of data (data starting from 0) traverses each row of the current column {Mergecellset (tdIdName1, i); }///<summary>//////</summary>///<param name= "tdIdName1" > ID text </param>///&l for the current row of the current column T;param name= "I" > Current line </param> private void Mergecellset (String Tdidname1, int i) {HtmlTableCell Cellprev = rptincome.items[i-1]. FindControl (tdIdName1) as HtmlTableCell; Gets the cell in which the next row is in the front of the TD HtmlTableCell Cell = Rptincome.items[i]. FindControl (tdIdName1) as HtmlTableCell; Gets the cell cell in which the TD is currently in the forefront of the business. RowSpan = (cell. RowSpan = =-1)? 1:cell. RowSpan; Gets the number of rows spanned by the current row Cellprev.rowspan = (Cellprev.rowspan = = 1)? 1:cellprev.rowspan; Gets the number of rows in the current column cell across the next row if (cell. InnerText = = Cellprev.innertext) {//Let the current cell of the next line span the number of rows + the number of rows in the current row Cellprev.rowspan = = Cell.
RowSpan; Cell. Visible = false; Hide the current line//key code, and then judge the merge cell method for the 2nd column.} Repeater Background Code

The above is a small set to introduce the ASP.net in the GridView repeating table column of the implementation of the merge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.