Source Code : date formatting in vb.net

 date formatting in vb.net


loading data into datagridview from xml file. And I have a one problem: date formatting.

Datagridview shows a date like this: 2011-01-01T00:00:00+02:00

How to force him to show the date in short date format (only 01.01.2011) without a time

I've tried this code:

  ds = New DataSet
  ds.ReadXml("filename.xml")

  Dim dv As DataView = New DataView(ds.Tables(0))

  DataGridView1.Columns.Clear()
  DataGridView1.DataSource = dv

  DataGridView1.Columns(1).DefaultCellStyle.Format = "d"
  DataGridView1.Update()

Sure it works.

Just this this simlpe example and it does work:

Public Partial Class Form1
   Inherits Form
   Private table As DataTable
   Public Sub New()
     InitializeComponent()
     Dim dgv As New DataGridView()
     Me.Controls.Add(dgv)
     table = New DataTable("MyTable")
     table.Columns.Add("Date", GetType(DateTime))
     table.Rows.Add(DateTime.Now)

     dgv.DataSource = New BindingSource(table, Nothing)
     dgv.Columns(0).DefaultCellStyle.Format = "dd/MM/yyyy"
   End Sub
End Class

Try it this way:

Private Sub Form1_Load(sender As Object, e As EventArgs)
   Dim datatable As New DataTable()
   datatable.Columns.Add("col1", GetType(DateTime))
   datatable.Columns.Add("col2", GetType(DateTime))
   datatable.Rows.Add(DateTime.Parse("2007/12/31 5:00:11 PM"), DateTime.Parse("2008/01/01 6:00:12 AM"))

   datatable.Rows.Add(DateTime.Parse("2007/4/4 1:00:13 AM"), DateTime.Parse("2007/8/8 3:00:14 PM"))

   dataGridView1.DataSource = datatable

   dataGridView1.Columns(0).DefaultCellStyle.Format = "dd'/'MM'/'yyyy"
   'Get 13/12/2007
   dataGridView1.Columns(1).DefaultCellStyle.Format = "dd'/'MM'/'yyyy hh:mm:ss tt"
   'Get 13/12/2007 5:00:11 PM
End Sub

Just this this simlpe example and it does work:

Public Partial Class Form1
   Inherits Form
   Private table As DataTable
   Public Sub New()
     InitializeComponent()
     Dim dgv As New DataGridView()
     Me.Controls.Add(dgv)
     table = New DataTable("MyTable")
     table.Columns.Add("Date", GetType(DateTime))
     table.Rows.Add(DateTime.Now)

     dgv.DataSource = New BindingSource(table, Nothing)
     dgv.Columns(0).DefaultCellStyle.Format = "dd/MM/yyyy"
   End Sub
End Class