ASP. NET for repeated column merge in the GridView table, asp. netgridview

Source: Internet
Author: User

ASP. NET for repeated column merge in the GridView table, 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:

1 <div> 2 <asp: GridView ID = "gvIncome" runat = "server" AutoGenerateColumns = "False"> 3 <Columns> 4 <asp: templateField HeaderText = "Level 1"> 5 <ItemTemplate> 6 <asp: Label ID = "Label0" runat = "server" Text = '<% # Eval ("aname ") %> '> </asp: Label> 7 </ItemTemplate> 8 </asp: TemplateField> 9 <asp: templateField HeaderText = "Level 2"> 10 <ItemTemplate> 11 <asp: Label ID = "Label1" runat = "server" Text = '<% # Eval ("bname ") %> '> </asp: Label> 12 </ItemTemplate> 13 </asp: TemplateField> 14 <asp: templateField HeaderText = "Level 3"> 15 <ItemTemplate> 16 <asp: Label ID = "Label2" runat = "server" Text = '<% # Eval ("cname ") %> '> </asp: Label> 17 </ItemTemplate> 18 </asp: TemplateField> 19 <asp: templateField HeaderText = "Level 4"> 20 <ItemTemplate> 21 <asp: Label ID = "Label3" runat = "server" Text = '<% # Eval ("dname ") %> '> </asp: Label> 22 </ItemTemplate> 23 </asp: TemplateField> 24 </Columns> 25 </asp: GridView> 26 </div>GridView front-end code
 

Background code:

1 public void DataBind () 2 {3 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 "; 4 SqlDataAdapter sda = new SqlDataAdapter (SQL, cn); 5 DataSet ds = new DataSet (); 6 sda. fill (ds); 7 gvIncome. dataSource = ds; 8 gvIncome. dataBind (); 9 // MergeRows (gvIncome. headerRow, gv Income. rows. count); 10 int colnum = gvIncome. columns. count; // obtain the number of columns in the GridView 11 MergeRows (gvIncome, 4, "Label "); // Lable control 12} 13 public static void MergeRows (GridView gvw, int colnum, string controlNameo) 14 {15 for (int col = 0; col <colnum; col ++) // traverse each column 16 {17 string controlName = controlNameo + col. toString (); // obtain the Lable control ID18 for (int rowIndex = gvw. rows. count- 2; rowIndex> = 0; rowIndex --) // retrieve the number of rows in the GridView and traverse each row 19 {20 GridViewRow row = gvw. rows [rowIndex]; // obtain the current row 21 GridViewRow previousRow = gvw. rows [rowIndex + 1]; // obtain the row 22 Label row_lbl = row. cells [col]. findControl (controlName) as Label; // obtain the text 23 Label previousRow_lbl = previousRow of the Lable Control ID of the current column. cells [col]. findControl (controlName) as Label; // obtain the text of the Lable Control ID of the previous row before the current column. 24 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 blank 25 {26 if (row_lbl.Text = previusrow_lbl.text) // if the text of the Lable ID to be modified for the current row and the previous row is not empty and the same 27 {28 // The current cell of the current row (the number of rows that the cell spans. 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 129 rows. cells [col]. rowSpan = previusrow. cells [col]. rowSpan <1? 2: previusrow. cells [col]. rowSpan + 1; 30 // and make the current cell of the previous row not show 31 previusrow. cells [col]. visible = false; 32} 33} 34} 35} 36 37}GridView background code

 

 

**************************************** ****************
* Gorgeous split line *
**************************************** ****************

Repeater:

Front-end code:

 

1 // table style 2 <style> 3 table {4 border-collapse: collapse; 5} 6 table tr td, th {7 border: 1px solid black; 8} 9 </style> 10 11 // ******************* 12 13 <div> 14 <table> 15 <tr> 16 <th> Level 1 </th> <th> Level 2 </th> <th> Level 3 </th> <th> Level 4 </th> 17 </tr> 18 <asp: repeater ID = "rptIncome" runat = "server"> 19 <ItemTemplate> 20 <tr> 21 <td runat = "server" id = "td0"> <% # Eval (" aname ") %> </td> 22 <td runat = "server" id = "td1"> <% # Eval ("bname ") %> </td> 23 <td runat = "server" id = "td2"> <% # Eval ("cname ") %> </td> 24 <td runat = "server" id = "td3"> <% # Eval ("dname ") %> </td> 25 </tr> 26 </ItemTemplate> 27 </asp: Repeater> 28 </table> 29 </div>Repeater front-end code

 

Background code:

1 public void DataBind () 2 {3 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 "; 4 SqlDataAdapter sda = new SqlDataAdapter (SQL, cn); 5 DataSet ds = new DataSet (); 6 sda. fill (ds); 7 rptIncome. dataSource = ds; 8 rptIncome. dataBind (); 9 10 for (int I = 0; I <4; I ++ )/ /Traverse each column 11 {12 string rpttd = "td"; 13 string tdIdName1 = rpttd + I. toString (); 14 MergeCell (tdIdName1 ); // use the ID Text of the current column as the method parameter 15} 16 17} 18 19 /// <summary> 20 /// 21 /// </summary> 22 /// <param name = "tdIdName1"> ID Text of the td before the current column </param> 23 private void MergeCell (string tdIdName1) 24 {25 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 of the current column 26 {27 Mer GeCellSet (tdIdName1, I ); 28} 29} 30 // <summary> 31 // 32 // </summary> 33 // <param name = "tdIdName1"> the current column is the td </param> 34 // <param name = "I"> current row </param> 35 private void MergeCellSet (string tdIdName1, int I) 36 {37 HtmlTableCell cellPrev = rptIncome. items [I-1]. findControl (tdIdName1) as HtmlTableCell; // obtains the cell 38 HtmlTableCell cell = rptIncome where the td in the current column of the next row is located. items [I]. findControl (tdIdName1) As HtmlTableCell; // obtain the 39 cell. RowSpan = (cell. RowSpan =-1) of the cell where the td is located at the top of the current row )? 1: cell. RowSpan; // obtain the number of rows in the current row when the number of rows in the front row is 40 cellPrev. RowSpan = (cellPrev. RowSpan =-1 )? 1: cellPrev. rowSpan; // obtain the number of rows that the current column of the next row spans. 41 if (cell. innerText = cellPrev. innerText) 42 {43 // set the number of rows in the current cell of the next row + the number of rows in the current row to 44 cellPrev. rowSpan + = cell. rowSpan; 45 cell. visible = false; // hide the current row 46 47 // key code, and then judge and execute the 2nd-column merge Cell Method 48} 49}Repeater background code

 

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.