In ASP. NET, how does one combine the duplicate data of the GridView and Repeater? gridviewrepeater

Source: Internet
Author: User

In ASP. NET, how does one combine the duplicate data of the GridView and Repeater? gridviewrepeater

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>

Copy codeThe Code is as follows: <span style = "line-height: 1.5; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; background-color: rgb (255,255,255); "> </span>
GridView 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 colnum = 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 ID that needs to be changed in the current column for (int rowIndex = gvw. rows. count-2; rowIndex> = 0; rowIndex --) // Gri The number of rows retrieved from the dashboard and traversed 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; // disable the current cell of the previous row from displaying previusrow. Cells [col]. Visible = false ;}}}}}

Front-end code of Repeater:

// 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>

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 rptt D = "td"; string 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> ///// </summa Ry> /// <param name = "tdIdName1"> ID Text of the td column 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 }}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.