Thursday, 16 May 2013

Merge 3 columns into 1 Column in GridView


Merge 3 columns into 1 Column in GridView


  • hi all,
    I have three bound columns in my gridview, namely :address 1, address 2, address 3.
    -  I want to have a single coulmn instead.
    - Where i want to display the text something like (address1 = address 1 + address 2+ address 3)
    - Also want to rename the  header 'address 1' to 'Bank Address'.
    - Moreover address 2 and address 3 columns should not be visible.
    What I tried is:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    { 
    GridView1.Columns[5].HeaderText = "Bank Address";
    e.Row.Cells[5].Text = e.Row.Cells[5].Text + e.Row.CellsDevil.Text + e.Row.Cells[7].Text;
    GridView1.ColumnsDevil.Visible = false;
    GridView1.Columns[7].Visible = false;
    }
    The above code is not giving the desired output.
    Regards,
    Royal
  • 5 years ago 
    hi royal ludhiana,According to me your code is altmost right but there is small problem with your code.

    [quote user="royal ludhiana"]protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e) 
    {
     
    GridView1.Columns[5].HeaderText = 
    "Bank Address";
     
    e.Row.Cells[5].Text = e.Row.Cells[5].Text + e.Row.CellsDevil.Text + e.Row.Cells[7].Text;
    GridView1.ColumnsDevil.Visible = 
    false;GridView1.Columns[7].Visible = false;}
    [/quote] 
    You can use this way
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    {
     
    GridView1.Columns[5].HeaderText = "Bank Address";
     
    e.Row.Cells[5].Text = e.Row.Cells[5].Text + e.Row.CellsDevil.Text + e.Row.Cells[7].Text;
    e.Row .Cells [5].ColumnSpan =3;}

    Hope this will help you.

No comments:

Post a Comment