How to retain merged cells in gridview during edit mode

Jan 11 2017 2:48 AM

i am merging cells with equal value (webform)

the merge cells are working, but how to retain merged cells during edit mode?

i am using the example template field from www.marss.co.ua/2010/01/how-to-merge-cells-with-equal-values-in.html 

i have not seen any sample code that supports edit mode

code behind 
  1. protected void gvAgentDueBalanceList_PreRender(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         //GridDecorator.MergeRows(gvAgentDueBalanceList);  
  6.         GridDecorator.MergeRowsForAgent(gvAgentDueBalanceList);  
  7.     }  
  8.     catch (Exception ex)  
  9.     {  
  10.         ErrorLog.LogNewError("DueBalanceList.aspx.cs; gvAgentDueBalanceList_PreRender;", ex);  
  11.         lblError.Text = "<br />Error gvAgentDueBalanceList_PreRender : " + ex.Message;  
  12.         //lblError.Text += "<br />StackTrace : " + ex.StackTrace.ToString();  
  13.     }  
  14. }  
  15.   
  16. public class GridDecorator  
  17. {  
  18.     public static void MergeRows(GridView gridView)  
  19.     {  
  20.         for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)  
  21.         {  
  22.             GridViewRow row = gridView.Rows[rowIndex];  
  23.             GridViewRow previousRow = gridView.Rows[rowIndex + 1];  
  24.   
  25.             for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)  
  26.             {  
  27.                 string text = ((Label)row.Cells[cellIndex].FindControl("Label" + cellIndex)).Text;  
  28.                 string previousText = ((Label)previousRow.Cells[cellIndex].FindControl("Label" + cellIndex)).Text;  
  29.   
  30.                 if (text == previousText)  
  31.                 {  
  32.                     row.Cells[cellIndex].RowSpan = previousRow.Cells[cellIndex].RowSpan < 2 ? 2 : previousRow.Cells[cellIndex].RowSpan + 1;  
  33.                     previousRow.Cells[cellIndex].Visible = false;  
  34.                 }  
  35.             } //end for  
  36.         } //end for  
  37.     }  
  38.   
  39.     public static void MergeRowsForAgent(GridView gridView)  
  40.     {  
  41.         int ErrorcellIndex = 0;  
  42.         try  
  43.         {  
  44.             for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)  
  45.             {  
  46.                 GridViewRow row = gridView.Rows[rowIndex];  
  47.                 GridViewRow previousRow = gridView.Rows[rowIndex + 1];  
  48.   
  49.                 for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)  
  50.                 {  
  51.                     ErrorcellIndex = cellIndex;  
  52.                     if (cellIndex == 5) //Action  
  53.                     {  
  54.                         //row.Cells[cellIndex].RowSpan = 1;  
  55.                         continue;  
  56.                     }  
  57.   
  58.                     if (cellIndex == 6) //Edit  
  59.                     {  
  60.                         //row.Cells[cellIndex].RowSpan = 1;  
  61.   
  62.                         if ((row.RowState == DataControlRowState.Normal  
  63.                             | row.RowState == DataControlRowState.Alternate)  
  64.                             &&  
  65.                             (previousRow.RowState == DataControlRowState.Normal  
  66.                             | previousRow.RowState == DataControlRowState.Alternate)  
  67.                             )  
  68.                         {  
  69.                             string textEdit = ((Label)row.Cells[cellIndex].FindControl("lblEditBagID")).Text;  
  70.                             string previousTextEdit = ((Label)previousRow.Cells[cellIndex].FindControl("lblEditBagID")).Text;  
  71.   
  72.                             if (textEdit == previousTextEdit)  
  73.                             {  
  74.                                 row.Cells[cellIndex].RowSpan = previousRow.Cells[cellIndex].RowSpan < 2 ? 2 : previousRow.Cells[cellIndex].RowSpan + 1;  
  75.                                 previousRow.Cells[cellIndex].Visible = false;  
  76.                             }  
  77.                         }  
  78.   
  79.                         if (row.RowState == DataControlRowState.Edit)  
  80.                         {  
  81.   
  82.                         }  
  83.   
  84.                         continue;  
  85.                     }  
  86.   
  87.                     if ((row.RowState == DataControlRowState.Normal  
  88.                         | row.RowState == DataControlRowState.Alternate)  
  89.                         &&  
  90.                         (previousRow.RowState == DataControlRowState.Normal  
  91.                         | previousRow.RowState == DataControlRowState.Alternate)  
  92.                         )  
  93.                     {  
  94.                         string text = ((Label)row.Cells[cellIndex].FindControl("Label" + cellIndex)).Text;  
  95.                         string previousText = ((Label)previousRow.Cells[cellIndex].FindControl("Label" + cellIndex)).Text;  
  96.   
  97.                         if (text == previousText)  
  98.                         {  
  99.                             row.Cells[cellIndex].RowSpan = previousRow.Cells[cellIndex].RowSpan < 2 ? 2 : previousRow.Cells[cellIndex].RowSpan + 1;  
  100.                             previousRow.Cells[cellIndex].Visible = false;  
  101.                         }  
  102.                     }  
  103.   
  104.                     if (row.RowState == DataControlRowState.Edit)  
  105.                     {  
  106.                     }  
  107.                 } //end for  
  108.             } //end for  
  109.         }  
  110.         catch (Exception ex)  
  111.         {  
  112.             ErrorLog.LogNewError("DueBalanceList.aspx.cs; MergeRowsForAgent;", ex);  
  113.             ErrorLog.LogFiler("DueBalanceList.aspx.cs; MergeRowsForAgent; StackTrace : " + ex.StackTrace.ToString());  
  114.             ErrorLog.LogFiler("DueBalanceList.aspx.cs; MergeRowsForAgent; ErrorcellIndex : " + ErrorcellIndex);  
  115.             throw ex;  
  116.             //lblError.Text += "<br />StackTrace : " + ex.StackTrace.ToString();  
  117.         }  
  118.     }  
  119. }  
 
aspx page 
  1. <asp:GridView ID="gvAgentDueBalanceList" runat="server" AutoGenerateColumns="False"  
  2. CellPadding="4" CellSpacing="2" Width="100%" AllowPaging="False"  
  3. PageSize="40" AllowSorting="True"  
  4. CssClass="Grid"  
  5. AlternatingRowStyle-CssClass="alt"  
  6. PagerStyle-CssClass="pgr"  
  7. OnPreRender="gvAgentDueBalanceList_PreRender"  
  8. OnRowDataBound="gvAgentDueBalanceList_RowDataBound"  
  9. OnRowCommand="gvAgentDueBalanceList_RowCommand"  
  10. OnRowCancelingEdit="gvAgentDueBalanceList_RowCancelingEdit"  
  11. OnRowEditing="gvAgentDueBalanceList_RowEditing"  
  12. OnRowUpdating="gvAgentDueBalanceList_RowUpdating"  
  13. OnRowDeleting="gvAgentDueBalanceList_RowDeleting"  
  14. EmptyDataText="No record found"   
  15. EnableModelValidation="True">  
  16. <Columns>  
  17.     <asp:TemplateField HeaderText="Bag User" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">  
  18.         <ItemStyle HorizontalAlign="Left" Width="90px" />  
  19.         <ItemTemplate>  
  20.             <%--  
  21.             <asp:Label ID="lblBagUser" runat="server" Text='<%# Eval("BagUser") %>'></asp:Label>  
  22.             --%>  
  23.             <asp:Label ID="Label0" runat="server" Text='<%# Eval("BagUser") %>'></asp:Label>  
  24.         </ItemTemplate>  
  25.     </asp:TemplateField>  
  26.     <asp:TemplateField HeaderText="Curr" ShowHeader="False" HeaderStyle-HorizontalAlign="Center">  
  27.         <ItemStyle HorizontalAlign="Center" Width="30px" />  
  28.         <ItemTemplate>  
  29.             <%--  
  30.             <asp:Label ID="lblCurrencyCode" runat="server" Text='<%# Eval("CurrencyCode2") %>'></asp:Label>  
  31.             --%>  
  32.             <asp:Label ID="Label1" runat="server" Text='<%# Eval("CurrencyCode2") %>'></asp:Label>  
  33.         </ItemTemplate>  
  34.     </asp:TemplateField>  
  35.     <asp:TemplateField HeaderText="Due Amount" ShowHeader="False" HeaderStyle-HorizontalAlign="Center">  
  36.         <ItemStyle HorizontalAlign="Center" Width="80px" />  
  37.         <ItemTemplate>  
  38.             <%--  
  39.             <asp:Label ID="lblTotalAmountDue" runat="server" Text='<%# Eval("TotalAmountDue", "{0:#,0.00}") %>'></asp:Label>  
  40.             --%>  
  41.             <asp:Label ID="Label2" runat="server" Text='<%# Eval("TotalAmountDue", "{0:#,0.00}") %>'></asp:Label>  
  42.         </ItemTemplate>  
  43.     </asp:TemplateField>  
  44.     <asp:TemplateField Visible="false" HeaderText="Agent SN" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">  
  45.         <ItemStyle HorizontalAlign="Left" />  
  46.         <ItemTemplate>  
  47.             <%--  
  48.             <asp:Label ID="lblBagID" runat="server" Text='<%# Eval("BagID") %>'></asp:Label>  
  49.             --%>  
  50.             <asp:Label ID="Label3" runat="server" Text='<%# Eval("BagID") %>'></asp:Label>  
  51.         </ItemTemplate>  
  52.     </asp:TemplateField>  
  53.     <asp:TemplateField Visible="false" HeaderText="SH ID" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">  
  54.         <ItemStyle HorizontalAlign="Left" Width="90px" />  
  55.         <ItemTemplate>  
  56.             <%--  
  57.             <asp:Label ID="lblShareID" runat="server" Text='<%# Eval("ShareID") %>'></asp:Label>  
  58.             --%>  
  59.             <asp:Label ID="Label4" runat="server" Text='<%# Eval("ShareID") %>'></asp:Label>  
  60.         </ItemTemplate>  
  61.     </asp:TemplateField>  
  62.     <%--  
  63.     --%>  
  64.     <asp:TemplateField HeaderText="Action" ShowHeader="False" HeaderStyle-HorizontalAlign="Center">  
  65.         <ItemStyle HorizontalAlign="Center" Width="100px" />  
  66.         <ItemTemplate>  
  67.             <asp:LinkButton ID="lnkBtnPayment" runat="server" CausesValidation="false" CommandName="Payment">Payment</asp:LinkButton>  
  68.             |  
  69.             <asp:LinkButton ID="lnkBtnView" runat="server" CausesValidation="false" CommandName="View">View</asp:LinkButton>  
  70.         </ItemTemplate>  
  71.     </asp:TemplateField>  
  72.     <%--  
  73.     --%>  
  74.     <asp:TemplateField HeaderText="Edit" ShowHeader="False" HeaderStyle-HorizontalAlign="Center">  
  75.         <ItemStyle HorizontalAlign="Center" Width="100px" />  
  76.         <ItemTemplate>  
  77.             <asp:Label Visible="false" ID="lblEditBagID" runat="server" Text='<%# Eval("BagID") %>'></asp:Label>  
  78.             <asp:LinkButton ID="lnkBtnEdit" runat="server" CausesValidation="false" CommandName="Edit">Edit</asp:LinkButton>  
  79.         </ItemTemplate>  
  80.         <EditItemTemplate>  
  81.             <asp:LinkButton ID="lnkBtnUpdate" runat="server" CausesValidation="false" CommandName="Update">Update</asp:LinkButton>  
  82.             |  
  83.             <asp:LinkButton ID="lnkBtnCancel" runat="server" CausesValidation="false" CommandName="Cancel">Cancel</asp:LinkButton>  
  84.         </EditItemTemplate>  
  85.     </asp:TemplateField>  
  86.     <asp:TemplateField HeaderText="Name" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">  
  87.         <ItemStyle HorizontalAlign="Left" Width="150px" />  
  88.         <ItemTemplate>  
  89.             <%--  
  90.             <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>  
  91.             --%>  
  92.             <asp:Label ID="Label7" runat="server" Text='<%# Eval("Name") %>'></asp:Label>  
  93.         </ItemTemplate>  
  94.         <EditItemTemplate>  
  95.             <asp:TextBox ID="txtGridEditName" runat="server" Text='<%# Bind("Name") %>' onKeypress="Javascript:if(window.event.keyCode==13){this.focus();return false;}" Width="130px" CssClass="tbEditModeStyle" MaxLength="35"></asp:TextBox>   
  96.         </EditItemTemplate>  
  97.     </asp:TemplateField>  
  98.     <asp:TemplateField HeaderText="Contact" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">  
  99.         <ItemStyle HorizontalAlign="Left"  />  
  100.         <ItemTemplate>  
  101.             <%--  
  102.             <asp:Label ID="lblContact" runat="server" Text='<%# Eval("Contact").ToString().Replace("\n", "<br />") %>'></asp:Label>  
  103.             --%>  
  104.             <asp:Label ID="Label8" runat="server" Text='<%# Eval("Contact").ToString().Replace("\n", "<br />") %>'></asp:Label>  
  105.         </ItemTemplate>  
  106.         <EditItemTemplate>  
  107.             <asp:TextBox ID="txtGridEditContact" runat="server" Text='<%# Bind("Contact") %>' TextMode="MultiLine" Height="40px" Width="92%" CssClass="tbEditModeStyle" MaxLength="255"></asp:TextBox>   
  108.         </EditItemTemplate>  
  109.     </asp:TemplateField>  
  110.     <asp:TemplateField HeaderText="Account Ref." ShowHeader="False" HeaderStyle-HorizontalAlign="Left">  
  111.         <ItemStyle HorizontalAlign="Left"  />  
  112.         <ItemTemplate>  
  113.             <%--  
  114.             <asp:Label ID="lblAccount" runat="server" Text='<%# Eval("Account").ToString().Replace("\n", "<br />") %>'></asp:Label>  
  115.             --%>  
  116.             <asp:Label ID="Label9" runat="server" Text='<%# Eval("Account").ToString().Replace("\n", "<br />") %>'></asp:Label>  
  117.         </ItemTemplate>  
  118.         <EditItemTemplate>  
  119.             <asp:TextBox ID="txtGridEditAccount" runat="server" Text='<%# Bind("Account") %>' TextMode="MultiLine" Height="40px" Width="92%" CssClass="tbEditModeStyle" MaxLength="255"></asp:TextBox>   
  120.         </EditItemTemplate>  
  121.     </asp:TemplateField>  
  122.     <asp:TemplateField HeaderText="Remarks" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">  
  123.         <ItemStyle HorizontalAlign="Left"   />  
  124.         <ItemTemplate>  
  125.             <%--  
  126.             <asp:Label ID="lblRemark" runat="server" Text='<%# Eval("Remark").ToString().Replace("\n", "<br />") %>'></asp:Label>  
  127.             --%>  
  128.             <asp:Label ID="Label10" runat="server" Text='<%# Eval("Remark").ToString().Replace("\n", "<br />") %>'></asp:Label>  
  129.         </ItemTemplate>  
  130.         <EditItemTemplate>  
  131.             <asp:TextBox ID="txtGridEditRemark" runat="server" Text='<%# Bind("Remark") %>' TextMode="MultiLine" Height="40px" Width="92%" CssClass="tbEditModeStyle" MaxLength="255"></asp:TextBox>   
  132.         </EditItemTemplate>  
  133.     </asp:TemplateField>  
  134. </Columns>