According to your description, do you display your picture in TemplateField?
If so, of course, they all have their own
imageURL attributes.
When you loop the cell, you can determine whether they contain child controls.
The image field is placed in TemplateField. When you loop to the cell where the image belongs, you can enter another judgment and decide whether to merge the cells by getting the same imageURL attribute of the image control.
For more details,you could refer to the following code:
Contributor
2820 Points
803 Posts
Re: Merging Cells with asp
Jul 03, 2019 07:36 AM|Yongqing Yu|LINK
Hi markdirtyboy,
According to your description, do you display your picture in TemplateField?
If so, of course, they all have their own imageURL attributes.
When you loop the cell, you can determine whether they contain child controls.
The image field is placed in TemplateField. When you loop to the cell where the image belongs, you can enter another judgment and decide whether to merge the cells by getting the same imageURL attribute of the image control.
For more details,you could refer to the following code:
My table structure:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmplId"> <Columns> <asp:BoundField DataField="EmplId" HeaderText="EmplId" ReadOnly="True" SortExpression="EmplId" /> <asp:TemplateField HeaderText="EmailAddr" SortExpression="EmailAddr"> <ItemTemplate> <asp:Image ID="Image1" runat="server" ImageUrl='<%# String.Concat("../Images1/",Eval("EmailAddr")) %>' Width="100px" Height="100px" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" /> <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" /> </Columns> </asp:GridView> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form> </body> </html>
code behind:
protected void Button1_Click(object sender, EventArgs e) { MergeCells(GridView1); } public static void MergeCells(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 i = 0; i < row.Cells.Count - 1; i++) { if (row.Cells[i].Controls.Count != 0 && previousRow.Cells[i].Controls.Count != 0) { Image image1 = row.Cells[i].FindControl("Image1") as Image; Image image2 = previousRow.Cells[i].FindControl("Image1") as Image; if (image1.ImageUrl == image2.ImageUrl) { row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : previousRow.Cells[i].RowSpan + 1; previousRow.Cells[i].Visible = false; } } else { if (row.Cells[i].Text == previousRow.Cells[i].Text) { row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : previousRow.Cells[i].RowSpan + 1; previousRow.Cells[i].Visible = false; } } } } }
Here is the result of my work demo:
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.