Member 12841980 Ответов: 0

Как сохранить объединенные ячейки в ASP.NET gridview (webform) в режиме редактирования


я объединяю ячейки с равным значением
я использую примерное поле шаблона из http://www.marss.co.ua/2010/01/how-to-merge-cells-with-equal-values-in.html

ячейки слияния работают, но как сохранить объединенные ячейки в режиме редактирования?

разметка aspx
<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">Payment</asp:LinkButton>
				|
				<asp:LinkButton ID="lnkBtnView" runat="server" CausesValidation="false" CommandName="View">View</asp: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">Edit</asp:LinkButton>
			</ItemTemplate>
			<EditItemTemplate>
				<asp:LinkButton ID="lnkBtnUpdate" runat="server" CausesValidation="false" CommandName="Update">Update</asp:LinkButton>
				|
				<asp:LinkButton ID="lnkBtnCancel" runat="server" CausesValidation="false" CommandName="Cancel">Cancel</asp: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", "<br />") %>'></asp:Label>
				--%>
				<asp:Label ID="Label8" runat="server" Text='<%# Eval("Contact").ToString().Replace("\n", "<br />") %>'></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", "<br />") %>'></asp:Label>
				--%>
				<asp:Label ID="Label9" runat="server" Text='<%# Eval("Account").ToString().Replace("\n", "<br />") %>'></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", "<br />") %>'></asp:Label>
				--%>
				<asp:Label ID="Label10" runat="server" Text='<%# Eval("Remark").ToString().Replace("\n", "<br />") %>'></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>
</asp:GridView>


с фоновым кодом
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 = "<br />Error gvAgentDueBalanceList_PreRender : " + ex.Message;
		//lblError.Text += "<br />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 += "<br />StackTrace : " + ex.StackTrace.ToString();
		}
	}
}


Тнх

Что я уже пробовал:

до сих пор я не нашел ни одного образца, который объединил бы ячейку с режимом редактирования

0 Ответов