Results 1 to 9 of 9

Thread: Grouping in data table ( removing duplicates )

  1. #1

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Grouping in data table ( removing duplicates )

    please excuse this thread is for / in connection with this thread

    in a datatble, say for example table 3 columns
    emp_id
    emp_name
    emp_class

    here i don't want field emp_id to repeat for each row,
    i mean i want to set empty value in the data table keeping only the first row entry
    this check & update needs to be done for each emp_id.

    i have done it by comparing each row with all the row and then setting the values to empty
    very nasty & resource consuming & time consuming & harder to code

    please some professional approach for this please.
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,509

    Re: Grouping in data table ( removing duplicates )

    If you setup the table with emp_id as the Primary Key, the database wont let you enter duplicate emp_id.

  3. #3

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Grouping in data table ( removing duplicates )

    no no,
    i am failed to visualize you, sorry

    lets say 2 tables

    table T1
    emp_id (Pk)
    emp_name
    emp_class

    table T2
    emp_id (Fk)
    Consumption
    Consumption_date

    now
    SELECT t1.emp_name,t2.consumption_date,t2.Consumption from t1 left join t2 on t1._emp_id = t2.emp_id group by t1.emp_id;
    in the query above t1.emp_name will repeat redundantly, this i want to eliminate so as to retain only one emp_name for all his relative records
    and then next emp_name & so on


    this query will be loaded in to the dataset, further i need help please
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Grouping in data table ( removing duplicates )

    What the ?....That shouldn't even be legal. SQL Server would never allow that as you need to aggregate non grouped fields. In such a case the name would not be repeated. I had no idea MySql allows that abomination....but then again, what do I know. I'm curious to see what the guys more experienced with different databases would have to say about this.
    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

  5. #5

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Grouping in data table ( removing duplicates )

    this is the query out put
    Attachment 97485
    i need to eliminate duplicate names in datatable or in the query & the out must be
    Attachment 97487
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Grouping in data table ( removing duplicates )

    Can't be done, shouldn't be done. Unless you're prepared to transfer to a listview and use that column as the group property on each item.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Grouping in data table ( removing duplicates )

    At the DataTable level, I would look at DataTable.Merge method to eliminate dups.
    Or perhaps use the following DataGridView to group by column
    Code:
    Public Class GroupByGrid
        Inherits DataGridView
    
        Protected Overrides Sub OnCellFormatting(ByVal args As DataGridViewCellFormattingEventArgs)
            MyBase.OnCellFormatting(args)
    
            ' First row always displays
            If args.RowIndex = 0 Then
                Return
            End If
    
            If IsRepeatedCellValue(args.RowIndex, args.ColumnIndex) Then
                args.Value = String.Empty
                args.FormattingApplied = True
            End If
        End Sub
        Private Function IsRepeatedCellValue(ByVal rowIndex As Integer, ByVal colIndex As Integer) As Boolean
            Dim currCell As DataGridViewCell = Rows(rowIndex).Cells(colIndex)
            Dim prevCell As DataGridViewCell = Rows(rowIndex - 1).Cells(colIndex)
    
            If (currCell.Value Is prevCell.Value) OrElse (currCell.Value IsNot Nothing AndAlso prevCell.Value IsNot Nothing AndAlso currCell.Value.ToString() = prevCell.Value.ToString()) Then
                Return True
            Else
                Return False
            End If
    
        End Function
        Protected Overrides Sub OnCellPainting(ByVal args As DataGridViewCellPaintingEventArgs)
    
            MyBase.OnCellPainting(args)
    
            args.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None
    
            ' Ignore column and row headers and first row
            If args.RowIndex < 1 OrElse args.ColumnIndex < 0 Then
                Return
            End If
    
            If IsRepeatedCellValue(args.RowIndex, args.ColumnIndex) Then
                args.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None
            Else
                args.AdvancedBorderStyle.Top = AdvancedCellBorderStyle.Top
            End If
    
        End Sub
    End Class
    Last edited by kareninstructor; Mar 8th, 2013 at 07:14 PM. Reason: Added more information

  8. #8

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Grouping in data table ( removing duplicates )

    kevin after long time i am getting you
    i need to do it in datatable only,
    a kind of looping + comparing prev row with next row + editing the datatble columns, it works but time consuming

    is the LINQ can simplify this task ?
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Grouping in data table ( removing duplicates )

    I would had thought the DataGridView I suggested would had done the job
    Attachment 97527

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