开发者

How to group data in datatable in c#

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  arj开发者_开发问答un
         manu
anitha   manju.k
         rahul
rohit    sandeep
         vinay
         anitha

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

Thank you.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜