Question Date field on export

Arg81

Well-known member
Joined
Mar 11, 2005
Messages
949
Location
Midlands, UK
Programming Experience
1-3
Good morning,

I am exporting a datagridview to a text file. The code to do this is:
VB.NET:
Public Sub WriteTextFile(ByVal dgv As DataGridView, ByVal varlbl As String)


        Dim strExport As String = ""
      [COLOR="seagreen"]  'Loop through all the columns in DataGridView to Set the Column Heading[/COLOR]        
        For Each dc As DataGridViewColumn In dgv.Columns
            strExport += dc.Name.Substring(0) & "*"
        Next

        strExport = strExport.Substring(0) + Environment.NewLine.ToString()
      [COLOR="seagreen"]  'Loop through all the row and append the value with 3 spaces[/COLOR]
        For Each dr As DataGridViewRow In dgv.Rows
            For Each dc As DataGridViewCell In dr.Cells
                If dc.Value IsNot Nothing Then
                    strExport += dc.Value.ToString() & "*"
                End If
            Next
            strExport += Environment.NewLine.ToString()
        Next
            [COLOR="seagreen"]  'Create a TextWrite object to write to file, varlbl is the txt file to export to[/COLOR]        
        Dim tw As System.IO.TextWriter = New System.IO.StreamWriter(varlbl)
        [COLOR="seagreen"]'Write the Text to file[/COLOR]
        tw.Write(strExport)
     [COLOR="SeaGreen"]   'Close the Textwrite[/COLOR]
        tw.Close()

    End Sub

One of the columns is a date field. Visually it displays the date how I want it (e.g. 18 December 2009)
however when it is exported to the text file the format is 18/12/2009 00:00:00

This text file is then used on a mail merge to a document to produce some labels.

Obviously I don't want the time shown.

Is there a way to remove this during the export?
 
If you call ToString on a Date, that's what you're going to get. If you want a specific format then you have to call ToShortDateString or the like, or else pass a format string to ToString, e.g. "d". That means you're not going to be able to treat every field the same.

Also, I'm guessing that your grid is bound. In that case it is considered good practice to work with the data source rather than the grid.
 
Hi JM,

Indeed the grid is bound.

I found the code on Google.... We use to have ComponentOne grids that allowed for export to a file, the standard WinGrid doesn't and the above does export it all.

I've looked at the datasource but it all seems OK, i.e. it doesn't show the time... unless I've missed something?? Haven't done any programming for a year or so and suddenly jumped back into it, so brain is a little rusty.

EDIT: just to clarify the above.
If I go to the datasource the column is System.DateTime.
If I preview the data the field is dd/mm/yyyy

The grid is set to only show dd/mm/yyyy - however when exported it also shows the time.
I'm assuming that's the "ToString" issue, but I'm not sure how to change just the one field to date from the code I'm using.
 
Last edited:
Back
Top