Asp. How to merge the GridView and repeater duplicate data in net _ practical tips

Source: Internet
Author: User
Tags dname eval


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="Secondary">
      <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="Four Levels">
      <ItemTemplate>
       <asp:Label ID="Label3" runat="server" Text='<%#Eval("dname") %>'></asp:Label>
      </ItemTemplate>
     </asp:TemplateField>
    </Columns>
   </asp:GridView>
  </div>




Copy Code code 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 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; // Get the number of columns in the GridView
   MergeRows(gvIncome, 4, "Label"); // GridView Number of columns to be integrated Lable control to be changed
  }
  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(); // Get the Lable control ID of the current column that needs to be changed
    For (int rowIndex = gvw.Rows.Count - 2; rowIndex >= 0; rowIndex--) //Get the number of rows in the GridView and traverse each row
    {
     GridViewRow row = gvw.Rows[rowIndex]; // Get the current row
     GridViewRow previousRow = gvw.Rows[rowIndex + 1]; // Get the previous row of the current row
     Label row_lbl = row.Cells[col].FindControl(controlName) as Label; //// Get the text of the Lable control ID of the current row of the current column
     Label previousRow_lbl = previousRow.Cells[col].FindControl(controlName) as Label; //// Get the text of the Lable control ID of the previous line of the current row of the current column
     If (row_lbl != null && previousRow_lbl != null) // If the current line and the previous line of the Lable's ID to be changed are not empty
     {
      If (row_lbl.Text == previousRow_lbl.Text) // If the current line and the previous line of the Lable are to be changed, the text of the ID is not empty and the same
      {
       // The current cell of the current row (the number of rows spanned by the cell. The default value is 0). The current cell of the next row is equal to the number of spanned rows and is less than one. Returns 2 otherwise the current cell of the previous row spans. Number of rows +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;
      }
     }
    }
   }
    
  }


 


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 Background Code:


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++) // traverse each column
   {
    String rpttd = "td";
    String tdIdName1 = rpttd + i.ToString();
    MergeCell(tdIdName1); // Use the ID text of the current column's td as the argument to the method
   }
    
  }
 
  /// <summary>
  ///
  /// </summary>
  /// <param name="tdIdName1">The ID text of the td of the current column of 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 rows of data (data starts at 0) Traverses each row of the current column
   {
    MergeCellSet(tdIdName1, i);
   }
  }
  /// <summary>
  ///
  /// </summary>
  /// <param name="tdIdName1">The ID text of the td of the current column of the current column</param>
  /// <param name="i">current line</param>
  Private void MergeCellSet(string tdIdName1, int i)
  {
   HtmlTableCell cellPrev = rptIncome.Items[i - 1].FindControl(tdIdName1) as HtmlTableCell; // Get the cell where the td of the current column of the next row is located
   HtmlTableCell cell = rptIncome.Items[i].FindControl(tdIdName1) as HtmlTableCell; // Get the cell where the current column of the current row is td
   cell.RowSpan = (cell.RowSpan == -1) ? 1 : cell.RowSpan; // Get the number of rows spanned by the current column cell of the current row
   cellPrev.RowSpan = (cellPrev.RowSpan == -1) ? 1 : cellPrev.RowSpan; // Get the number of rows spanned by the current row of cells in the next row
   If (cell.InnerText == cellPrev.InnerText)
   {
    // Let the current row of the next row span the number of rows + the current row spans the number of rows
    cellPrev.RowSpan += cell.RowSpan;
    cell.Visible = false; // hide the current line
 
    / / key code, then judge the implementation of the second column of the merged cell method
   }
  }


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.


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.