show same date data in 1 row

budihermawan

New member
Joined
Apr 9, 2010
Messages
2
Programming Experience
1-3
I have simple problem but until now i still cannot figure it out.

I have data in the database as follows :

2010-02-01 07:40
2010-02-01 11:30
2010-02-02 08:03
2010-02-02 01:40

I need to display the data in gridview or reportviewer as follow:

2010-02-01 07:40 11:30
2010-02-01 08:03 01:40

Can somehow enlighten me on how to arrange the data in 1 row based on date in gridview or reportviewer.
A sample source code would be highly appreciated.
 
Your example is just an example. It doesn't tell us what the rules are governing how the data is to be transformed. Are we to assume that there will always be two records per date? Do you always want the first time in the second column and the second time in the third column?
 
It's called a pivot query, but it doesnt seem to have a header

You might find it easier to pivot the data manually yourself

Make a datatable in the designer that has a DAte primary key row, I'll call it PivoterTable, it has one column called theDate:

VB.NET:
'get the data
myTableAdapterX.Fill(myXTable)

Dim pT as New PivoterTable

For Each xRo as XDataRow in myXTable
  Dim pRo as PivoterDataRow = pT.FindByTheDate(xRo.TheDate)
  If pRo is Nothing Then
    pT.AddNewPivoterRow(xRo.TheDate, xRo.TheTime)
  Else
    pRo.TheTimes += xRo.TheTime
  End If
Next


If you want the times to appear in separate columns:

VB.NET:
'get the data
myTableAdapterX.Fill(myXTable)

Dim pT as New PivoterTable

For Each xRo as XDataRow in myXTable
  Dim pRo as PivoterDataRow = pT.FindByTheDate(xRo.TheDate)
  If pRo is Nothing Then
    pT.AddNewPivoterRow(xRo.TheDate, xRo.TheTime)
  Else
    Dim i as Integer = 1
    While i < pT.Columns.Count-1 AndAlso Not pRo.IsNull(i)
      i += 1
    End While
    If i = pT.Columns.Count Then
      pT.Columns.Add("Time"+i)
    End If
    pRo(i) = xRo.TheTime
  End If
Next

Note, this code is untested!
 
PS; investigate if your DB can do a pivot with no header row first, as it may save you some headache
 
Back
Top