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- protected void gvAgentDueBalanceList_PreRender(object sender, EventArgs e)
- {
- try
- {
- //GridDecorator.MergeRows(gvAgentDueBalanceList);
- GridDecorator.MergeRowsForAgent(gvAgentDueBalanceList);
- }
- catch (Exception ex)
- {
- ErrorLog.LogNewError("DueBalanceList.aspx.cs; gvAgentDueBalanceList_PreRender;", ex);
- lblError.Text = "
Error gvAgentDueBalanceList_PreRender : " + ex.Message; - //lblError.Text += "
StackTrace : " + ex.StackTrace.ToString(); - }
- }
- public class GridDecorator
- {
- public static void MergeRows(GridView gridView)
- {
- for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
- {
- GridViewRow row = gridView.Rows[rowIndex];
- GridViewRow previousRow = gridView.Rows[rowIndex + 1];
- for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
- {
- string text = ((Label)row.Cells[cellIndex].FindControl("Label" + cellIndex)).Text;
- string previousText = ((Label)previousRow.Cells[cellIndex].FindControl("Label" + cellIndex)).Text;
- if (text == previousText)
- {
- row.Cells[cellIndex].RowSpan = previousRow.Cells[cellIndex].RowSpan < 2 ? 2 : previousRow.Cells[cellIndex].RowSpan + 1;
- previousRow.Cells[cellIndex].Visible = false;
- }
- } //end for
- } //end for
- }
- public static void MergeRowsForAgent(GridView gridView)
- {
- int ErrorcellIndex = 0;
- try
- {
- for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
- {
- GridViewRow row = gridView.Rows[rowIndex];
- GridViewRow previousRow = gridView.Rows[rowIndex + 1];
- for (int cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
- {
- ErrorcellIndex = cellIndex;
- if (cellIndex == 5) //Action
- {
- //row.Cells[cellIndex].RowSpan = 1;
- continue;
- }
- if (cellIndex == 6) //Edit
- {
- //row.Cells[cellIndex].RowSpan = 1;
- if ((row.RowState == DataControlRowState.Normal
- | row.RowState == DataControlRowState.Alternate)
- &&
- (previousRow.RowState == DataControlRowState.Normal
- | previousRow.RowState == DataControlRowState.Alternate)
- )
- {
- string textEdit = ((Label)row.Cells[cellIndex].FindControl("lblEditBagID")).Text;
- string previousTextEdit = ((Label)previousRow.Cells[cellIndex].FindControl("lblEditBagID")).Text;
- if (textEdit == previousTextEdit)
- {
- row.Cells[cellIndex].RowSpan = previousRow.Cells[cellIndex].RowSpan < 2 ? 2 : previousRow.Cells[cellIndex].RowSpan + 1;
- previousRow.Cells[cellIndex].Visible = false;
- }
- }
- if (row.RowState == DataControlRowState.Edit)
- {
- }
- continue;
- }
- if ((row.RowState == DataControlRowState.Normal
- | row.RowState == DataControlRowState.Alternate)
- &&
- (previousRow.RowState == DataControlRowState.Normal
- | previousRow.RowState == DataControlRowState.Alternate)
- )
- {
- string text = ((Label)row.Cells[cellIndex].FindControl("Label" + cellIndex)).Text;
- string previousText = ((Label)previousRow.Cells[cellIndex].FindControl("Label" + cellIndex)).Text;
- if (text == previousText)
- {
- row.Cells[cellIndex].RowSpan = previousRow.Cells[cellIndex].RowSpan < 2 ? 2 : previousRow.Cells[cellIndex].RowSpan + 1;
- previousRow.Cells[cellIndex].Visible = false;
- }
- }
- if (row.RowState == DataControlRowState.Edit)
- {
- }
- } //end for
- } //end for
- }
- catch (Exception ex)
- {
- ErrorLog.LogNewError("DueBalanceList.aspx.cs; MergeRowsForAgent;", ex);
- ErrorLog.LogFiler("DueBalanceList.aspx.cs; MergeRowsForAgent; StackTrace : " + ex.StackTrace.ToString());
- ErrorLog.LogFiler("DueBalanceList.aspx.cs; MergeRowsForAgent; ErrorcellIndex : " + ErrorcellIndex);
- throw ex;
- //lblError.Text += "
StackTrace : " + ex.StackTrace.ToString(); - }
- }
- }
aspx page
- <asp:GridView ID="gvAgentDueBalanceList" runat="server" AutoGenerateColumns="False"
- CellPadding="4" CellSpacing="2" Width="100%" AllowPaging="False"
- PageSize="40" AllowSorting="True"
- CssClass="Grid"
- AlternatingRowStyle-CssClass="alt"
- PagerStyle-CssClass="pgr"
- OnPreRender="gvAgentDueBalanceList_PreRender"
- OnRowDataBound="gvAgentDueBalanceList_RowDataBound"
- OnRowCommand="gvAgentDueBalanceList_RowCommand"
- OnRowCancelingEdit="gvAgentDueBalanceList_RowCancelingEdit"
- OnRowEditing="gvAgentDueBalanceList_RowEditing"
- OnRowUpdating="gvAgentDueBalanceList_RowUpdating"
- OnRowDeleting="gvAgentDueBalanceList_RowDeleting"
- EmptyDataText="No record found"
- EnableModelValidation="True">
- <Columns>
- <asp:TemplateField HeaderText="Bag User" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
- <ItemStyle HorizontalAlign="Left" Width="90px" />
- <ItemTemplate>
- <%--
- <asp:Label ID="lblBagUser" runat="server" Text='<%# Eval("BagUser") %>'>asp:Label>
- --%>
- <asp:Label ID="Label0" runat="server" Text='<%# Eval("BagUser") %>'>asp:Label>
- ItemTemplate>
- asp:TemplateField>
- <asp:TemplateField HeaderText="Curr" ShowHeader="False" HeaderStyle-HorizontalAlign="Center">
- <ItemStyle HorizontalAlign="Center" Width="30px" />
- <ItemTemplate>
- <%--
- <asp:Label ID="lblCurrencyCode" runat="server" Text='<%# Eval("CurrencyCode2") %>'>asp:Label>
- --%>
- <asp:Label ID="Label1" runat="server" Text='<%# Eval("CurrencyCode2") %>'>asp:Label>
- ItemTemplate>
- asp:TemplateField>
- <asp:TemplateField HeaderText="Due Amount" ShowHeader="False" HeaderStyle-HorizontalAlign="Center">
- <ItemStyle HorizontalAlign="Center" Width="80px" />
- <ItemTemplate>
- <%--
- <asp:Label ID="lblTotalAmountDue" runat="server" Text='<%# Eval("TotalAmountDue", "{0:#,0.00}") %>'>asp:Label>
- --%>
- <asp:Label ID="Label2" runat="server" Text='<%# Eval("TotalAmountDue", "{0:#,0.00}") %>'>asp:Label>
- ItemTemplate>
- asp:TemplateField>
- <asp:TemplateField Visible="false" HeaderText="Agent SN" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
- <ItemStyle HorizontalAlign="Left" />
- <ItemTemplate>
- <%--
- <asp:Label ID="lblBagID" runat="server" Text='<%# Eval("BagID") %>'>asp:Label>
- --%>
- <asp:Label ID="Label3" runat="server" Text='<%# Eval("BagID") %>'>asp:Label>
- ItemTemplate>
- asp:TemplateField>
- <asp:TemplateField Visible="false" HeaderText="SH ID" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
- <ItemStyle HorizontalAlign="Left" Width="90px" />
- <ItemTemplate>
- <%--
- <asp:Label ID="lblShareID" runat="server" Text='<%# Eval("ShareID") %>'>asp:Label>
- --%>
- <asp:Label ID="Label4" runat="server" Text='<%# Eval("ShareID") %>'>asp:Label>
- ItemTemplate>
- asp:TemplateField>
- <%--
- --%>
- <asp:TemplateField HeaderText="Action" ShowHeader="False" HeaderStyle-HorizontalAlign="Center">
- <ItemStyle HorizontalAlign="Center" Width="100px" />
- <ItemTemplate>
- <asp:LinkButton ID="lnkBtnPayment" runat="server" CausesValidation="false" CommandName="Payment">Paymentasp:LinkButton>
- |
- <asp:LinkButton ID="lnkBtnView" runat="server" CausesValidation="false" CommandName="View">Viewasp:LinkButton>
- ItemTemplate>
- asp:TemplateField>
- <%--
- --%>
- <asp:TemplateField HeaderText="Edit" ShowHeader="False" HeaderStyle-HorizontalAlign="Center">
- <ItemStyle HorizontalAlign="Center" Width="100px" />
- <ItemTemplate>
- <asp:Label Visible="false" ID="lblEditBagID" runat="server" Text='<%# Eval("BagID") %>'>asp:Label>
- <asp:LinkButton ID="lnkBtnEdit" runat="server" CausesValidation="false" CommandName="Edit">Editasp:LinkButton>
- ItemTemplate>
- <EditItemTemplate>
- <asp:LinkButton ID="lnkBtnUpdate" runat="server" CausesValidation="false" CommandName="Update">Updateasp:LinkButton>
- |
- <asp:LinkButton ID="lnkBtnCancel" runat="server" CausesValidation="false" CommandName="Cancel">Cancelasp:LinkButton>
- EditItemTemplate>
- asp:TemplateField>
- <asp:TemplateField HeaderText="Name" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
- <ItemStyle HorizontalAlign="Left" Width="150px" />
- <ItemTemplate>
- <%--
- <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'>asp:Label>
- --%>
- <asp:Label ID="Label7" runat="server" Text='<%# Eval("Name") %>'>asp:Label>
- ItemTemplate>
- <EditItemTemplate>
- <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>
- EditItemTemplate>
- asp:TemplateField>
- <asp:TemplateField HeaderText="Contact" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
- <ItemStyle HorizontalAlign="Left" />
- <ItemTemplate>
- <%--
- <asp:Label ID="lblContact" runat="server" Text='<%# Eval("Contact").ToString().Replace("\n", "
") %>'>asp:Label> - --%>
- <asp:Label ID="Label8" runat="server" Text='<%# Eval("Contact").ToString().Replace("\n", "
") %>'>asp:Label> - ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox ID="txtGridEditContact" runat="server" Text='<%# Bind("Contact") %>' TextMode="MultiLine" Height="40px" Width="92%" CssClass="tbEditModeStyle" MaxLength="255">asp:TextBox>
- EditItemTemplate>
- asp:TemplateField>
- <asp:TemplateField HeaderText="Account Ref." ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
- <ItemStyle HorizontalAlign="Left" />
- <ItemTemplate>
- <%--
- <asp:Label ID="lblAccount" runat="server" Text='<%# Eval("Account").ToString().Replace("\n", "
") %>'>asp:Label> - --%>
- <asp:Label ID="Label9" runat="server" Text='<%# Eval("Account").ToString().Replace("\n", "
") %>'>asp:Label> - ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox ID="txtGridEditAccount" runat="server" Text='<%# Bind("Account") %>' TextMode="MultiLine" Height="40px" Width="92%" CssClass="tbEditModeStyle" MaxLength="255">asp:TextBox>
- EditItemTemplate>
- asp:TemplateField>
- <asp:TemplateField HeaderText="Remarks" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
- <ItemStyle HorizontalAlign="Left" />
- <ItemTemplate>
- <%--
- <asp:Label ID="lblRemark" runat="server" Text='<%# Eval("Remark").ToString().Replace("\n", "
") %>'>asp:Label> - --%>
- <asp:Label ID="Label10" runat="server" Text='<%# Eval("Remark").ToString().Replace("\n", "
") %>'>asp:Label> - ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox ID="txtGridEditRemark" runat="server" Text='<%# Bind("Remark") %>' TextMode="MultiLine" Height="40px" Width="92%" CssClass="tbEditModeStyle" MaxLength="255">asp:TextBox>
- EditItemTemplate>
- asp:TemplateField>
- Columns>
Replies
Know the answer? Post it — somebody with the same question will find it here.