Implementation of repeated table column merge in GridView in ASP. NET, asp. netgridview

Source: Internet
Author: User
Tags dname

Implementation of repeated table column merge in GridView in ASP. NET, asp. netgridview

In the past few days, a project can be used to display data in a table. When the customer asks that duplicate data columns need to be merged, the Repeater and GridView methods for repeated data merging are summarized.

As follows:

GridView:

Front-end code:

<Div> <asp: GridView ID = "gvIncome" runat = "server" AutoGenerateColumns = "False"> <Columns> <asp: templateField HeaderText = "Level 1"> <ItemTemplate> <asp: Label ID = "Label0" runat = "server" Text = '<% # Eval ("aname ") %> '> </asp: Label> </ItemTemplate> </asp: TemplateField> <asp: TemplateField HeaderText = "Level 2"> <ItemTemplate> <asp: label ID = "Label1" runat = "server" Text = '<% # Eval ("bname") %>'> </asp: label> </ItemTemplate> </asp: TemplateField> <asp: TemplateField HeaderText = "Level 3"> <ItemTemplate> <asp: label ID = "Label2" runat = "server" Text = '<% # Eval ("cname") %>'> </asp: label> </ItemTemplate> </asp: TemplateField> <asp: TemplateField HeaderText = "Level 4"> <ItemTemplate> <asp: label ID = "Label3" runat = "server" Text = '<% # Eval ("dname") %>'> </asp: label> </ItemTemplate> </asp: TemplateField> </Columns> </asp: GridView> </div> front-end code of the GridView
<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. aname, B. bname, c. cname, d. dname from aa as a right join bb as B on. 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. 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 col Num = gvIncome. columns. count; // get the number of columns in the GridView MergeRows (gvIncome, 4, "Label "); // Lable control to be changed for the number of columns to be integrated in the GridView} public static void MergeRows (GridView gvw, int colnum, string controlNameo) {for (int col = 0; col <colnum; col ++) // traverse each column {string controlName = controlNameo + col. toString (); // obtain the Lable control IDfor (int rowIndex = gvw. rows. count-2; rowIndex> = 0; rowIndex --) // retrieve the number of rows in the GridView and traverse each row {GridViewRow row = gvw. rows [rowIndex]; // obtain the current row GridViewRow previousRow = gvw. rows [rowIndex + 1]; // obtain the Label row_lbl = row of the previous row. cells [col]. findControl (controlName) as Label; // obtain the text Label previousRow_lbl = previousRow of the Lable Control ID of the current column. cells [col]. findControl (controlName) as Label; // obtain the text if (row_lbl! = Null & previusrow_lbl! = Null) // if the text of the Lable ID to be modified in the current row and the previous row is not empty {if (row_lbl.Text = previusrow_lbl.text) // if the text of the Lable ID to be modified in the current row and the previous row is not empty and the same {// current cell of the current row (number of rows crossed by cells. The default value is 0). If it is equal to the number of rows in the current cell of the next row and less than one, 2 is returned. Otherwise, the number of rows in the current cell of the previous row is more than 1 row. cells [col]. rowSpan = previusrow. cells [col]. rowSpan <1? 2: previusrow. cells [col]. rowSpan + 1; // and make the current cell of the previous row not show previusrow. cells [col]. visible = false ;}}}} GridView background code

Repeater:

Front-end code:

// Table style <style> table {border-collapse: collapse;} table tr td, th {border: 1px solid black ;} </style> // ****************** <div> <table> <tr> <th> Level 1 </th> <th> Level 2 </th> <th> Level 3 </th> <th> Level 4 </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> front-end code of Repeater

Background code:

Public void DataBind () {string SQL = "select. aname, B. bname, c. cname, d. dname from aa as a right join bb as B on. 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. 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"; s Tring tdIdName1 = rpttd + I. toString (); MergeCell (tdIdName1 ); // use the ID Text of the td in the current column as the method parameter }/// <summary> //// </summary> /// <param name =" tdIdName1 "> ID Text of the td before the current column </param> private void MergeCell (string tdIdName1) {for (int I = rptIncome. items. count-1; I> 0; I --) // rptIncome. items. count-1 Total number of data rows (data starts from 0) traverse each row in the current column {MergeCellSet (tdIdName1, I );}} /// <summary> ///// </summary> /// <param name = "tdId Name1 "> ID Text of the td before the current column </param> // <param name =" I "> current row </param> private void MergeCellSet (string tdIdName1, int I) {HtmlTableCell cellPrev = rptIncome. items [I-1]. findControl (tdIdName1) as HtmlTableCell; // obtain the HtmlTableCell cell where the td in the current column of the next row is located = rptIncome. items [I]. findControl (tdIdName1) as HtmlTableCell; // gets the cell where the td is located at the top of the current row. rowSpan = (cell. rowSpan =-1 )? 1: cell. RowSpan; // obtain the number of rows in the current row when the number of cells in the front row spans cellPrev. RowSpan = (cellPrev. RowSpan =-1 )? 1: cellPrev. rowSpan; // obtain the number of rows that the current column of the next row spans. if (cell. innerText = cellPrev. innerText) {// set the number of rows in the current cell of the next row + the number of rows in the current row to cellPrev. rowSpan + = cell. rowSpan; cell. visible = false; // hide the current row // key code, and then judge and execute the 2nd-column merge Cell Method} Repeater background code

The above is a small series of ASP. in NET, the implementation of repeated table column merge in the GridView is helpful. If you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

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.