Re: combine datagrid columns

Discussion in 'ASP .Net' started by Davide Vernole [MVP], Sep 8, 2004.

  1. IGotYourDotNet <> typed:
    > I'm using a datagrid on my page and I'm getting my data from a web
    > service dataset. I need to combine 2 columns and display them as 1
    > column in the grid, also I have 1 column that is returned as 2 words
    > such as (chevy garage) i need to remove the word garage and only
    > display chevy, is there a way to do both of these?


    You could do that in the method delegate for the ItemDataBound event. Try
    this:

    if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType ==
    ListItemType.AlternatingItem))
    {
    //Combine two fields in one column
    string firstField =
    ((DataRowView)e.Item.DataItem).Row["FirstColumn"].ToString() ;
    string secondField =
    ((DataRowView)e.Item.DataItem).Row["SecondColumn"].ToString() ;
    e.Item.Cells[0].Text = firstField + " " + secondField;

    //Replace a field value before insert it in a cell
    string combinedField =
    ((DataRowView)e.Item.DataItem).Row["CombineColumn"].ToString() ;
    e.Item.Cells[1].Text = combinedField.Replace("Garage",
    string.Empty).Trim();
    }

    where:
    Cells[0] = Cell where you want to place the value of two field
    Cells[1] = Cell where you want to place the cleaned value of a combine field

    HTH
    --
    Davide Vernole
    MVP ASP/ASP.NET
    Microsoft Certified Solution Developer
     
    Davide Vernole [MVP], Sep 8, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CuMPeEWeEr [No MCSD]

    DataGrid problem - Combine 2 fields

    CuMPeEWeEr [No MCSD], Jan 6, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    662
    Lars Netzel
    Jan 7, 2005
  2. DBLWizard
    Replies:
    10
    Views:
    751
    Brock Allen
    Apr 2, 2005
  3. loudking
    Replies:
    1
    Views:
    382
    Bergamot
    Jun 4, 2007
  4. rob thomson
    Replies:
    0
    Views:
    264
    rob thomson
    Sep 4, 2003
  5. ton
    Replies:
    2
    Views:
    207
    Alvin Bruney [MVP]
    Feb 11, 2004
Loading...

Share This Page