Datagrid column formatting question.

spebola

Well-known member
Joined
Mar 9, 2003
Messages
50
Location
Oklahoma City
I am using a datagrid with one of the columns containing a date.
I would like this column to only contain the day of the month.

Day Price
2 30.00
6 32.00

instead of

01/02/2003 30.00
01/06/2003 32.00

This is the code I am using:

dgPrEntry.TableStyles.Clear()
dgPrEntry.DataSource = ds.Tables(0)
Dim ts1 As New DataGridTableStyle
ts1.MappingName = ds.Tables(0).ToString
Dim TC0 As New DataGridTextBoxColumn
TC0.MappingName = "PR_EffDate"
TC0.HeaderText = "Day"
TC0.Width = 80
ts1.GridColumnStyles.Add(TC0)

Any suggestions?
 
Three options come to mind:

(i) Perhaps there is a .Format value usable here. You should find a complete description of this property.

(ii) You can subclass a DataTextColumn and override its paint method.

(iii) Make the field as a calculated field in your database.
 
Another option is to, before binding the DataTable to the DataGrid,
is to loop through all the rows and modify the Day column to only
include the date. You can convert the date to a DateTime variable
by using DateTime.Parse(). The day of month is in
its Day property, then just set this back to the column.

Code:
Dim dr As DataRow

For Each dr in ds.Tables(0)  Loop through each row  
  Dim theDate As DateTime = DateTime.Parse(dr.Item(0).ToString())  Assuming 0 is the Day column, set it to a DT variable
  dr.Item(0) = theDate.Day  Set the day of month as the new value
Next
 
Back
Top