Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have a Gridview in which I need to merge cells of my last row (Not the Footer).
I have Googled everything and found no solution regarding this issue as there are many solutions to merge cells vertically or merging headers or footers.

My GridView:

| Task No | User Name | Subject | Request Type | Reference File |
| 123456 | Mr.XYZABC | DEMO | Testing Req | No Files Found |
| Remark | This is the cell I need to merge on all columns. |
| Task No | User Name | Subject | Request Type | Reference File |
| 123457 | Mr.AAAAAA | DEMO2 | Testing Req | No Files Found |
| Remark | This is the cell I need to merge on all columns. |
| Task No | User Name | Subject | Request Type | Reference File |
| 123458 | Mr.XXXXXX | DEMO3 | Testing Req | No Files Found |
| Remark | This is the cell I need to merge on all columns. |

On one button Click I am Binding 3 rows each time in a same gridview.
1st Row: 05 Columns
2nd Row: 05 Columns
3rd Row: 02 Columns (Remarks , This is the cell I need .....[Colspan=4])

In this example you can see that on one button click I am generating 3 rows and the last row cells should be merged.


Will be glad for the finest and best solution. Help Please. :)

Thanks & Regards,
AR.
Posted
Updated 5-Jun-14 19:57pm
v3

Finest and best solution is already available on internet. Please use Google.

Hint: In RowDataBound Event of GridView, you are suppose to loop through Cells of row . If you find text similar,merge them. Start this from rowIndex of 1 because you will not be merging the Header[Better to say ignore header in the Event],right? Let me know if you need more clarification on this.

Regards..
 
Share this answer
 
v3
Comments
AR547 6-Jun-14 2:19am    
Rohan,
Got your point. But I am not comparing any cell having same values, actually I need to create a 3 row with 2 columns in which I have to bind data from a textbox outside the gridview. But I tried that trick of applying 1 in rowIndex in RowDataBound but it was creating extra duplicate rows of that 3rd row.
:(
Thanks7872 6-Jun-14 3:29am    
I think you are tyring to merge 2nd and 3rd cells regardless of its same values. If so,just merge them without comparing their values. Whats the issue?
AR547 6-Jun-14 4:02am    
Almost done..but cannot generate new rows on next click. it is merging cells on each row now.
here is my code in RowDataBound:

protected void GV_Requests_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState != DataControlRowState.Edit) // check for RowState
{
if (e.Row.RowType == DataControlRowType.DataRow) //check for RowType
{
//Loop through all the cells in the row
for (int j = 0; j < GV_Requests_Sys.Rows.Count+1; j++)
{
if (j % 2 !=0)
{
e.Row.Cells[0].Text = "Request<br/>Description";
e.Row.Cells[0].ColumnSpan = 2;
e.Row.Cells[1].ColumnSpan = 4;
for (int i =1; i < e.Row.Cells.Count - 1; i++)
{
Checking If current cell is same as next cell value
if (e.Row.Cells[i].Text == e.Row.Cells[i + 1].Text)
{
e.Row.Cells[i + 1].Visible = false;
}
}
}
}
}
}
}
Hello freind, you may try this code:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.RowIndex > 0)
        {
            int colSpanValue = 2;
            //Loop through all the cells in the row
            for (int i = 0; i < e.Row.Cells.Count - 1; i++)
            {
                //Checking If current cell is same as next cell value
                if (e.Row.Cells[i].Text == e.Row.Cells[i + 1].Text)
                {
                    e.Row.Cells[i].ColumnSpan = colSpanValue;
                    e.Row.Cells[i + 1].Visible = false;
                    colSpanValue++;
                }
            }
        }
    }
}
 
Share this answer
 
Comments
AR547 6-Jun-14 2:23am    
Dear Friend,
This code will work if we have to compare the same values of cells. But data is coming from DataTable. The 3rd Row's first column's text is hard coded as Remarks and the 2nd column will have Random Text binded from textbox outside the gridview. The other 3rd 4th and 5th column should be merged.
Debabrata_Das 6-Jun-14 2:29am    
Hi,

It will merge the columns whenever there are consecutive cells with same values. I think it will work for your requirement. I would suggest you to give a try and then let me know your findings.

- DD
AR547 6-Jun-14 2:34am    
And how will I create a new row after data has been bound. Like Headers(1st row) and 2nd row created and now I need to create new row of Remarks and then I will check those cells with null values and will use your code here.
Debabrata_Das 6-Jun-14 3:30am    
Hi,
Let's not jump directly into the actual problem. First try with a simple POC where you can achieve cells merging functionality. Once you are successful, you'll feel more confident and you can implement the same logic into your real program.

Please note the code I shared is not going to solve your problem 100%. It's just a direction and then you have to tweak as per your requirement. :)

- DD
AR547 6-Jun-14 4:02am    
Almost done..but cannot generate new rows on next click. it is merging cells on each row now.
here is my code in RowDataBound:

protected void GV_Requests_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState != DataControlRowState.Edit) // check for RowState
{
if (e.Row.RowType == DataControlRowType.DataRow) //check for RowType
{
//Loop through all the cells in the row
for (int j = 0; j < GV_Requests_Sys.Rows.Count+1; j++)
{
if (j % 2 !=0)
{
e.Row.Cells[0].Text = "Request<br/>Description";
e.Row.Cells[0].ColumnSpan = 2;
e.Row.Cells[1].ColumnSpan = 4;
for (int i =1; i < e.Row.Cells.Count - 1; i++)
{
Checking If current cell is same as next cell value
if (e.Row.Cells[i].Text == e.Row.Cells[i + 1].Text)
{
e.Row.Cells[i + 1].Visible = false;
}
}
}
}
}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900