|
-
Mar 7th, 2013, 09:18 PM
#1
Thread Starter
PowerPoster
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.
-
Mar 7th, 2013, 10:18 PM
#2
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.
-
Mar 8th, 2013, 09:47 AM
#3
Thread Starter
PowerPoster
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
-
Mar 8th, 2013, 11:24 AM
#4
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.
-
Mar 8th, 2013, 11:49 AM
#5
Thread Starter
PowerPoster
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
-
Mar 8th, 2013, 06:02 PM
#6
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!
-
Mar 8th, 2013, 07:11 PM
#7
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
-
Mar 8th, 2013, 09:20 PM
#8
Thread Starter
PowerPoster
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 ?
-
Mar 10th, 2013, 12:29 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|