0

I have a datatable like this(I am getting the data from an excel sheet). I am converting the excel sheet to a datatable. Now i need to format the data. This is what my datatable looks like.

Lead    EMPnames
vinay    kumar
vinay    manju.u
vinay    kiran
anitha   manju.k
anitha   rahul
sandeep  arjun
sandeep  manu
rohit     sandeep
rohit     vinay
rohit     anitha

Now I need to format the datatable like this :

Lead     EMPnames
vinay    kumar
         manju.u
         kiran
sandeep  arjun
         manu
anitha   manju.k
         rahul
rohit    sandeep
         vinay
         anitha

Any idea as how to do this? Any help would be appreciated

Thank you.

1
  • What do you mean with formatting? Formatting it to the screen? Commented Aug 5, 2010 at 7:29

2 Answers 2

1

Why don't you use linq with the "group by" clause?

For example please see the sample 3 in the following link :

http://msdn.microsoft.com/en-us/vcsharp/aa336754.aspx#simple1

Sign up to request clarification or add additional context in comments.

1 Comment

Just to keep things up-to-date, the above link now redirects to general 101 LINQ. That is why it is always better to also show code samples in an answer rather than just posting a link. To find the code pointed out by the old link, go to the grouping section and click any of the samples listed there.
0

We have take the lead names in an array.

string[] srLead = { "vinay", "sandeep", "anitha", "rohit" };

then take the source datatable and filter it based on the lead names and add to a new DataTable:

    #region Grouping by Teamlead
    DataTable dtGroup = new DataTable();
    dtGroup = dtResult.Clone();
    foreach (DataColumn dc in dtResult.Columns)
    {
        dc.DataType = typeof(string);
    }
    dtGroup.AcceptChanges();

    foreach (string s in srLead)
    {
        string name = s;
        DataTable dtsource = new DataTable();
        dtsource = TeamLeadFilter(dtResult, name);
        CombineDatatable(ref dtGroup, dtsource);
        dtGroup.AcceptChanges();
    }
    #endregion

    #region TeamLeadFilter
    public DataTable TeamLeadFilter(DataTable dtResult, string str)
    {
        DataView dvData = new DataView(dtResult);
        dvData.RowFilter = "TeamLead ='" + str + "'";
        return dvData.ToTable();
    }
    #endregion

    #region CombineDatatable
    public DataTable CombineDatatable(ref DataTable destini, DataTable source)
    {
        foreach (DataRow dr in source.Rows)
        {
            destini.ImportRow(dr);
        }
        return destini;
    }
    #endregion  

by this way i solved the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.