Results 1 to 7 of 7

Thread: How to break down a DataTable into groups of rows(ones with same traits under Column

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2012
    Posts
    49

    How to break down a DataTable into groups of rows(ones with same traits under Column

    Hi guys,
    I have a Datatable with one column of the table being 'PartNum(product part number)'. I used EntriesTable.DefaultView.Sort = "PartNum ASC"
    to sort the whole Table, with respect to the information in this column. Now I'm just trying to further sort the table by grouping rows that have same info under that column together and do something to each of such groups. but I'm kinda stuck here.

    I'm thinking of creating some sub-DataTables out of the original one and have these sub-tables to hold those rows with same info under "PartNum".
    Does anyone have ideas how to do that? maybe with DataReader or DataView , I think??

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: How to break down a DataTable into groups of rows(ones with same traits under Col

    Sounds like you want an aggregate. What exactly do you want to do to each group ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: How to break down a DataTable into groups of rows(ones with same traits under Col

    If you want grouping, then that can be done with the "Select" statement that loads the datatable.
    something like,
    Code:
    "Select PartNum, Sum( cost ) from YourTableName Group By PartNum Order By PartNum"

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to break down a DataTable into groups of rows(ones with same traits under Col

    It sounds like all you need is a DataView. Simply create a new DataView for that DataTable, or use the DefaultView if appropriate, and then set the Filter property. That won't affect the DataTable itself but the DataView will hide all records that do not match the condition. The Select method of the DataTable can work in a similar way but return an array of DataRows instead.

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2012
    Posts
    49

    Re: How to break down a DataTable into groups of rows(ones with same traits under Col

    Thanks for the replies. After grouping by this trait, there's a third column that I need to check and combine some rows with respect to the info under that third column.
    But this PartNum column has higher priority so i want to do it first

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to break down a DataTable into groups of rows(ones with same traits under Col

    We need to know whether you want to aggregate or partition the data in order to describe how to do it. It sounded to me like you wanted to partition originally but now it seems like you might want to aggregate. Please provide a full and clear description of exactly what you want to achieve.

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2012
    Posts
    49

    Re: How to break down a DataTable into groups of rows(ones with same traits under Col

    Sorry about the confusion. Below are some samples from my table to illustrate what i want to do

    PartName|Reference Designator| PartNum | Manufacturer (and few other information..)
    x|C201 |02300877 |Samsung |….
    x|C201 |02300877 |Toshiba |….
    x|C207 |02301165 |....
    x|C209 |02301165 |….
    x|C213 |02300877 |Samsung
    x|C213 |02300877 |Toshiba
    x| C303 |02301163
    x| C305 |02301163
    x|C606 |02300877 |Samsung
    x|C606 |02300877 |Toshiba
    x|C708 |02301165
    x|C712 |02301163

    Primarily I'm trying to group entries with same PartNum together. This part can be done, as jmcilhinney suggested, by using DataView(DefaultView) to seperate the table into many(3,for the above sample's case)sub-tables. After I got the sub-tables, I want to go into each subtable and combine similar entries, as they only differ by Reference Designator now(I'll worry about difference in Manufacturer info later). Finally I would join everything back to one big DataTable now that they are properly sorted, grouped and combined. So part of the final Table would look like

    x| C201,C213,C606,C619 |02300877
    x| C303, C305,C712 |02301163
    x| C207, C209, C708 |02301165

    I think the confusion about whether I want to partition or aggregate arose as i only mentioned half of what I'm trying to do. Suggestions on the first half to partition or the second part to combine similar entries are all appreciated. Thanks guys.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width