Home » Asp.netRSS

How to merge gridview columns (cells)

 

How to merge gridview columns in a grid when the items in the grid are duplicate

 i mean in one row  four columns are there  i need to merge the columns which are repeating

eg:   Mango  Mango Mango Apple 

i want  like:    Mango           Apple

i the columns mango is getting repeated i need to merge it into one

 

Can anyone Help me!!!!!!!

 

 

11 Answers Found

 

Answer 1

can you elaborate bit more? 

if i understand it right.

1) you want to merging 3 columns. if the data in cells are duplicate? or 2) you want to merge 3 columns?

 

Answer 2

 i want to merge the cells in a row which is having duplicate values

if suppose in my grid  iam having three columns which are repeating i want to make it into one

How??????? 

Mango Mango Mango Apple

Apple Apple Apple Mango

above mango is repeating in row onei and Apple is repeating in row 2

i want to merge repeating columns into one.

 

Mango      Apple

Apple       Mango

 

Hope you Got it !!!!!!

Thanking you in Advance........

 
 

Answer 4

 the following post what you have sent is for row merging i want column merging in a grid

 

i want columns merging not row

 

Thanks in Advance

 

Answer 5

Hi .

You can use ColumnSpan property of Table Cell.

after you are done binding the data in grid and showing it. You can loop through thr rows and columns and check if the next column has the same value, then you can merge them..

something like this...


foreach(GridViewRow gvr in MyGridView.Rows)
            {
                foreach(TableCell tblCell in gvr.Cells)
                {
                    // check the values 
                    // if they are same for the next columns then use
                    tblCell.ColumnSpan
                }
            }


Hope it helps..

 

Answer 6

The best and low cost way will be doing it ion your sql query.

but you can do this by adding Template column in GridView and add 3 lables in it, that will be bound to Col1, Col2 and Col3. or Fetch the data from database as

SELECT Name,Col1 + ',' + Col2 + ',' + Col2 As Code From Table and use this Code column to bind with gridviews' column.  

or

To merge column header in a gridview you can use OnRowCreated Event of Grid View.

if (e.Row.RowType == DataControlRowType.Header)

{

//Build own custom header.

GridView oGridView = (GridView)sender;

GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);

TableCell oTableCell = new TableCell();

 

//Add Department

oTableCell.Text = "Department";

oTableCell.ColumnSpan = 2;

oGridViewRow.Cells.Add(oTableCell);

 

//Add Employee

oTableCell = new TableCell();

oTableCell.Text = "Employee";

oTableCell.ColumnSpan = 3;

oGridViewRow.Cells.Add(oTableCell);

oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);

}

 

Answer 7

 Still i am not getting.............the columns are not getting merged

 

Answer 8

check this out, in this post there are examples to merge columns/headers and merge cells (custom merge.)

http://forums.asp.net/t/1094937.aspx

else

please post your code.



 

Answer 9

System.Data.

dt.Columns.Add(

dt.Columns.Add(

dt.Columns.Add(

dt.Columns.Add(

System.Data.

dr[

dr[

dr[

dr[

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[

dr[

dr[

dr[

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[

dr[

dr[

dr[

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[

dr[

dr[

dr[

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[

dr[

dr[

dr[

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[

dr[

dr[

dr[

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[

dr[

dr[

dr[

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[

dr[

dr[

dr[

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[

dr[

dr[

dr[

dt.Rows.Add(dr);

GridView1.DataSource = dt;

GridView1.DataBind();

}

DataTable dt = new System.Data.DataTable();"this");"is");"a");"test");DataRow dr = dt.NewRow();"this"] = "data1";"is"] = "data1";"a"] = "data1";"test"] = "data1";"this"] = "data1";"is"] = "data1";"a"] = "data1";"test"] = "data1";"this"] = "data1";"is"] = "data1";"a"] = "data1";"test"] = "data1";"this"] = "data2";"is"] = "data2";"a"] = "data2";"test"] = "data2";"this"] = "data2";"is"] = "data2";"a"] = "data2";"test"] = "data2";"this"] = "data3";"is"] = "data3";"a"] = "data3";"test"] = "data3";"this"] = "data4";"is"] = "data4";"a"] = "data4";"test"] = "data4";"this"] = "data4";"is"] = "data4";"a"] = "data4";"test"] = "data4";"this"] = "data4";"is"] = "data4";"a"] = "data4";"test"] = "data4";

 

Answer 10

Test with my algo, then give feedback. Hopefully it will be done.

 

Answer 11

where is your algo???????

i have tried with your method also but it was not coming 

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter