Datagrid Date/Time how to show?

  • Thread starter Thread starter Roger
  • Start date Start date
R

Roger

All,

I am having a difficult time understanding how we can define columns in a
datagrid when we assign the data vai recordsource.

I have entered the properties of the Grid, and added a tableStyle. In this
style I have added same number of columns that are in
the return of the SQL statement below. I don't see these setting showing up
(I.E) I have changed the columnn names and don't
see these changes.

I also would like to see the full date and time not just the date and don't
know how to make this happen.

Here is the code.

Dim sSql As String
Dim cn As New SqlClient.SqlConnection("Initial Catalog=tmsystem;Data
Source=echo;Integrated Security=SSPI")
sSql = "SELECT DISTINCT tmQueue.dbo.machines.MachineName as Machine,
MAX(siteimports.Requested) AS [Last Process Date]" & _
" FROM tmQueue.dbo.machines INNER JOIN siteimports ON
tmQueue.dbo.machines.MachineKey = siteimports.ImportMachine" & _
" GROUP BY tmQueue.dbo.machines.MachineName"
Dim siteda As SqlDataAdapter = New SqlDataAdapter(sSql, cn)
Dim ds As New DataSet
siteda.Fill(ds, "Roger")
dgProcessingMachines.SetDataBinding(ds, "Roger")


Thanks,

Rog
 
I have changed the columnn names and don't
see these changes.

Don't have VS in front of me, but sounds like you forgot to set the
datatable name "Roger" in the MappingName property of the
DataGridTableStyle.

http://msdn.microsoft.com/msdnmag/issues/03/08/DataGrids/default.aspx

Greg

Roger said:
All,

I am having a difficult time understanding how we can define columns in a
datagrid when we assign the data vai recordsource.

I have entered the properties of the Grid, and added a tableStyle. In
this
style I have added same number of columns that are in
the return of the SQL statement below. I don't see these setting showing
up
(I.E) I have changed the columnn names and don't
see these changes.

I also would like to see the full date and time not just the date and
don't
know how to make this happen.

Here is the code.

Dim sSql As String
Dim cn As New SqlClient.SqlConnection("Initial
Catalog=tmsystem;Data
Source=echo;Integrated Security=SSPI")
sSql = "SELECT DISTINCT tmQueue.dbo.machines.MachineName as
Machine,
MAX(siteimports.Requested) AS [Last Process Date]" & _
" FROM tmQueue.dbo.machines INNER JOIN siteimports ON
tmQueue.dbo.machines.MachineKey = siteimports.ImportMachine" & _
" GROUP BY tmQueue.dbo.machines.MachineName"
Dim siteda As SqlDataAdapter = New SqlDataAdapter(sSql, cn)
Dim ds As New DataSet
siteda.Fill(ds, "Roger")
dgProcessingMachines.SetDataBinding(ds, "Roger")


Thanks,

Rog
 
Roger,

I have a GridFormatter class with a Format method that I use to format
datetime values from a datetable for display in a dategrid.

I send the Format method a datagrid and a datatable. The Format method
builds the table styles for the grid based on the columns of the datatable
and then assigns the datatable as the grid's datasource.

You might be able to modify the code for your needs:

Public Class GridFormatter

Public Shared Sub Format(ByRef grd As DataGrid, ByRef dt As DataTable)

Dim ts As New DataGridTableStyle()
Dim tc As DataGridTextBoxColumn

Dim dc As DataColumn
For Each dc In dt.Columns
tc = New DataGridTextBoxColumn()
tc.MappingName = dc.ColumnName
tc.HeaderText = tc.MappingName

If dc.DataType Is GetType(System.DateTime) Then
tc.Format = "MM/dd/yyyy hh:mm:ss"
tc.Width = 125
End If

ts.GridColumnStyles.Add(tc)
Next

grd.TableStyles.Clear()
grd.TableStyles.Add(ts)

grd.DataSource = Nothing
grd.DataSource = dt

End Sub

End Class

I call the Format method like this:

GridFormatter.Format(grdReservations, ReservationsDataTable)

Note that I compile this code with Option Strict Off. You might need to do
more explicit casting if you have option strict on.

Kerry Moorman
 
Back
Top