Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

ASP.ner / VB.net / VS2010

i would want to merge the column in my gridview where all four cells of a row are the same.

Could someone help me on this

thanks
Joe
Posted
Comments
Sergey Alexandrovich Kryukov 4-Oct-11 15:59pm    
And if some of the values become different, do you also want to "un-merge"? :-). Why do you really need all that? To me it sounds way to artificial...
--SA
joe_j 5-Oct-11 4:33am    
well its like, if all the 4 cells in the row have the "true" value, then the 4 cells merge and it should just show a single cell displaying the time that all 4 fields became "true".

Try this code.
Once you spanned the table cell, we should remove others.
C#
public partial class _Default : System.Web.UI.Page
    {
        private bool _alternate = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            ds.Tables.Add(new DataTable("Employee"));
            ds.Tables[0].Columns.Add("Id");
            ds.Tables[0].Columns.Add("Name");
            ds.Tables[0].Rows.Add("1", "Name1");
            ds.Tables[0].Rows.Add("1", "Name2");
            ds.Tables[0].Rows.Add("2", "Name3");
            ds.Tables[0].Rows.Add("2", "Name4");

            GridView1.DataSource = ds;
            GridView1.DataMember = "Employee";
            GridView1.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
        }

        void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (_alternate)
            {
                e.Row.Cells[0].ColumnSpan = 2;
                e.Row.Cells.RemoveAt(1);
            }

            _alternate = !_alternate;
        }
    }
 
Share this answer
 
v2
Comments
joe_j 6-Oct-11 14:38pm    
Pretty good Rafeeque, i think i got it. thanks man
In the DataBound event you can set the span

C#
gvRow.Cells[0].RowSpan
 
Share this answer
 
v2
Comments
joe_j 5-Oct-11 4:34am    
i did try this, however, i couldnt see it merging, it still shows all the 4 cells.
Rafeeque B Muhammad 5-Oct-11 5:04am    
look at my below solution..
This [^] should help you get started.
 
Share this answer
 
use <asp:templatefield> and merge n numbers of rows in one row
 
Share this answer
 
Comments
joe_j 5-Oct-11 4:59am    
it should only merge if the 4 cells in a row have the same data which is pulled from an access DB.
Sergey Alexandrovich Kryukov 15-Mar-13 14:10pm    
This can be considered as the answer, but I had to remove your other answers for the abuse.

Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).

Also, keep in mind that members only get notifications on the post sent in reply to there posts.

—SA

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