Merge 3 columns into 1 Column in GridView

  • 13 years ago

    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

  • 13 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.
    Thanks & Regards
    Dilipv

  • 13 years ago

    hi DilipV,

    I had already tried this i.e. columnspan this also doesn't give the desired output.

    I think its something to do with the bound column. But I am trying to trace it.

     

    Regards,

    Royal

  • 13 years ago

    Royal,

    I take it that returning the required combination address as part of the data is not 'optional'?

    As another option, how about returning the data as in a custom object that you can then use as a datasource and thereby manipulate the 'columns' as you like.. or would you prefer to stick to bound columns from a datatable/dataset?

    Joe
     

  • 13 years ago

    Alternatively, add a template column into your grid as follows:

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="address" HeaderText="address" visible="false"/>
                    <asp:BoundField DataField="city" HeaderText="city" visible="false"/>
                    <asp:BoundField DataField="region" HeaderText="region" visible="false"/>
                    <asp:BoundField DataField="postalcode" HeaderText="postalcode" visible="false"/>
                    <asp:TemplateField HeaderText="Full Address">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text="Label">
                                <%# Eval("address") + ", " + Eval("city") + ", " + Eval("region") + ", " + Eval("postalcode")%>
                            </asp:Label>                       
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

    then all you need to do is return the rquired "bound" data, and let the template do the rest:

            private void LoadEmployeeAddress()
            {
                string con = ConfigurationManager.AppSettings["NorthwindDatabase"];
                string sql = "select address, city, region, postalcode from employees;";

                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                da.Fill(ds);

                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
            }

    Joe
     

  • 13 years ago

    hi Joe,

    Right on Target Wink Yes

    Regards,

    Royal

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“PHP is a minor evil perpetrated and created by incompetent amateurs, whereas Perl is a great and insidious evil perpetrated by skilled but perverted professionals.” - Jon Ribbens