Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET
i have the read the some value from the database in data Table in C#.NET.
in this datatable the following value contains
batch  tag     pcdatelogged
1      11   12-12-2012 11:35
1      12   12-12-2012 11:36
1      13   12-12-2012 11:37
1      14   12-12-2012 11:38
2      15   12-12-2012 11:39
2      16   12-12-2012 11:40
2      17   12-12-2012 11:41
2      18   12-12-2012 11:42
i want to select the distinct values from the datatable & display into the grid
batch  tag(last value)   pcdatelogged(min)   pcdatelogged(max)
1           14           12-12-2012 11:35    12-12-2012 11:38
2           18           12-12-2012 11:39    12-12-2012 11:42
i want the above output in the grid
 

Thanks in advance
Posted 30-Dec-11 18:25pm
Edited 30-Dec-11 18:29pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi vrushali,
Try this code, i think it'll be helpful for you.
 
ds.Tables["datatablename"].DefaultView.ToTable(true, "batch"); 
dt.DefaultView.ToTable(true, "batch");
 
Quote:
where ds is a Dataset object.

 
Regards,
Bluesathish
  Permalink  
Comments
vrushali katkade at 31-Dec-11 1:08am
   
thanks for the replay ,what about another columns, its only distinct the batch column
bluesathish at 31-Dec-11 1:12am
   
Then try this one,
DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "Column1", "Column2" ...);
 
ref: http://stackoverflow.com/questions/1199176/how-to-select-distinct-values-from-datatable
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

select distinct * from datatablename
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Here is one solution :
 
if OBJECT_ID('tempdb..#t') is not null
  drop table #t
select  * , ROW_NUMBER() over ( partition by batch order by batch, tag) no  into #t from  events
 
select s.batch, tag, min , max from 
(select max(no) no, MIN(pcdatelogged) min , MAX(pcdatelogged) max , batch from #t group by batch) s
join #t on #t.no = s.no and #t.batch = s.batch
 

Convert it to a stored procedure and retrieve data with a suitable datasource.
 
Hope it helps.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 255
1 Sergey Alexandrovich Kryukov 104
2 Richard MacCutchan 100
3 kbrandwijk 75
4 Vinay Mistry 70
0 Sergey Alexandrovich Kryukov 9,040
1 OriginalGriff 8,121
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 11 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100