Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
2.54/5 (3 votes)
See more:
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
Updated 23-Sep-19 3:25am
v2

Hi vrushali,
Try this code, i think it'll be helpful for you.

SQL
ds.Tables["datatablename"].DefaultView.ToTable(true, "batch"); 
dt.DefaultView.ToTable(true, "batch");


Quote:
where ds is a Dataset object.


Regards,
Bluesathish
 
Share this answer
 
Comments
vrushali katkade 31-Dec-11 1:08am    
thanks for the replay ,what about another columns, its only distinct the batch column
bluesathish 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
Here is one solution :

SQL
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.
 
Share this answer
 
select distinct * from datatablename
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900