locked
Merging Cells with asp RRS feed

  • Question

  • User-1634117515 posted

    Hi,

    I´m merging some cells on one grid but the first row has images and when I do the merge only one image is showing but I have different images in some cases the image is repeated and I want to show only one in that case 

    here is the code 

      public 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 = 2; i < row.Cells.Count - 1; i++)
                    {
                            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;
                            }
                        
                    }
                }
            }

    Tuesday, July 2, 2019 5:46 PM

Answers

  • User665608656 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 3, 2019 7:36 AM

All replies

  • User665608656 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 3, 2019 7:36 AM
  • User-1634117515 posted

    Hi Yongqing Yu

    Thank you very much, it's working fine! 

    Wednesday, July 3, 2019 1:52 PM