Merge Column In GridView ASP.NET

DataGrid View is used to display the information in alternate rows and if the rows are empty, then no columns are visible. You can also use different colors for the alternate rows but it's another task.

Here, we are dynamically merging the column in GridView. 

 

The above picture shows that if the row is empty, then we need to merge all the columns. Also, not to show the cell border.
  1. <asp:UpdatePanel runat="server" ID="updatedata">  
  2.     <ContentTemplate>  
  3.         <div class="row text-center">  
  4.             <asp:Label ID="lblMsg" runat="server" EnableViewState="false"></asp:Label>  
  5.         </div>  
  6.         <asp:GridView ID="grd_data" runat="server" ViewStateMode="Disabled" AutoGenerateColumns="false" Width="100%" Height="100%" AlternatingRowStyle-BackColor="#CDFFFF" HeaderStyle-Font-Italic="true" OnRowDataBound="GridDatabound" RowStyle-CssClass="RowStyle" HeaderStyle-HorizontalAlign="Center" GridLines="Both" CssClass="griedline col-12" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="45" overflow="auto">  
  7.             <HeaderStyle HorizontalAlign="Center" CssClass="grdheader" />  
  8.             <Columns>  
  9.                 <asp:BoundField HeaderText="LINE" DataField="line" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />  
  10.                 <asp:BoundField HeaderText="MAINT" DataField="maint" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />  
  11.                 <asp:BoundField HeaderText="W D." DataField="wd" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />  
  12.                 <asp:BoundField HeaderText="STORE" DataField="stores" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />  
  13.                 <asp:BoundField HeaderText="QUALITY" DataField="quality" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />  
  14.                 <%-- <asp:TemplateField>  
  15. <HeaderTemplate><p> <h1> <i>Line</i></h1 ></p></HeaderTemplate>  
  16. <ItemTemplate>  
  17. <h1> <i><center> <asp:Label ID="lbl_line" runat="server" Text='<%#Eval("line") %>'></asp:Label>  
  18.                     </center>  
  19.                     </i>  
  20.                     </h1>  
  21.                     </ItemTemplate>  
  22.                     </asp:TemplateField>  
  23.                     <asp:TemplateField>  
  24.                         <HeaderTemplate>  
  25.                             <h1><i> Maint.</i></h1>  
  26.                         </HeaderTemplate>  
  27.                         <ItemTemplate>  
  28.                             <h1>  
  29.                                 <center>  
  30.                                     <asp:Label ID="lbl_maint" runat="server" Text='<%# Eval("maint") %>'></asp:Label>  
  31.                                 </center>  
  32.                             </h1>  
  33.                         </ItemTemplate>  
  34.                     </asp:TemplateField>  
  35.                     <asp:TemplateField>  
  36.                         <HeaderTemplate>  
  37.                             <h1><i>WD.</i></h1>  
  38.                         </HeaderTemplate>  
  39.                         <ItemTemplate>  
  40.                             <h1>  
  41.                                 <center>  
  42.                                     <asp:Label ID="lbl_wd" runat="server" Text='<%# Eval("wd") %>'></asp:Label>  
  43.                                 </center>  
  44.                             </h1>  
  45.                         </ItemTemplate>  
  46.                     </asp:TemplateField>  
  47.                     <asp:TemplateField>  
  48.                         <HeaderTemplate>  
  49.                             <h1><i> Stores</i></h1>  
  50.                         </HeaderTemplate>  
  51.                         <ItemTemplate>  
  52.                             <h1>  
  53.                                 <center>  
  54.                                     <asp:Label ID="lbl_stores" runat="server" Text='<%# Eval("stores") %>'></asp:Label>  
  55.                                 </center>  
  56.                             </h1>  
  57.                         </ItemTemplate>  
  58.                     </asp:TemplateField>  
  59.                     <asp:TemplateField>  
  60.                         <HeaderTemplate>  
  61.                             <h1><i>Quality</i></h1>  
  62.                         </HeaderTemplate>  
  63.                         <ItemTemplate>  
  64.                             <h1>  
  65.                                 <center>  
  66.                                     <asp:Label ID="lbl_quality" runat="server" Text='<%# Eval("quality") %>'></asp:Label>  
  67.                                 </center>  
  68.                             </h1>  
  69.                         </ItemTemplate>  
  70.                     </asp:TemplateField>--%> </Columns>  
  71.         </asp:GridView>  
  72.         <%--<asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="updatetimer_Tick" >--%> </asp:Timer>  
  73.     </ContentTemplate>  
  74. </asp:UpdatePanel>  
  75. private void mergecolumn() {  
  76.     for (int i = 0; i < grd_data.Rows.Count; i++) {  
  77.         if (i % 2 == 0) {  
  78.             for (int j = 0; j < grd_data.Rows[i].Cells.Count; j++) {  
  79.                 grd_data.Rows[i].Cells[j].Visible = false;  
  80.                 grd_data.Rows[i].Attributes.Add("colspan""5");  
  81.             }  
  82.         }  
  83.     }  
  84. }