Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Default datetime.now in datatable and bound datagridview

Default datetime.now in datatable and bound datagridview

Scheduled Pinned Locked Moved C#
databasehelpquestion
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L LordZoster

    Hallo my application sports a dgv bound to a datatable, which has a column with default value to DateTime.Now. When the user add a new row, that datetime column is correctly filled; when the user saves for the first time, it correctly saves. Any next time the user try saving the row, it gives a System.FormatException "String not recognized as valid DateTime value", and stops saving. The DateTime in the DGV appears to be formatted according the locale Culture (italian, dd/MM/yyyy hh.mm) The DataTable.Locale is System.Globalization.CultureInfo.InvariantCulture. If I change it to System.Globalization.CultureInfo.CurrentCulture i get an obscure exception "Found unknown word starting at index 0". Any help? Thanks in advance!

    A Offline
    A Offline
    Andy_L_J
    wrote on last edited by
    #2

    Do you have a DateTimePicker in the DataGridViewCell or is it a default DataGridViewCell (Text) Field? You may need to convert the value to a valid Date Type.

    I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife

    L 1 Reply Last reply
    0
    • L LordZoster

      Hallo my application sports a dgv bound to a datatable, which has a column with default value to DateTime.Now. When the user add a new row, that datetime column is correctly filled; when the user saves for the first time, it correctly saves. Any next time the user try saving the row, it gives a System.FormatException "String not recognized as valid DateTime value", and stops saving. The DateTime in the DGV appears to be formatted according the locale Culture (italian, dd/MM/yyyy hh.mm) The DataTable.Locale is System.Globalization.CultureInfo.InvariantCulture. If I change it to System.Globalization.CultureInfo.CurrentCulture i get an obscure exception "Found unknown word starting at index 0". Any help? Thanks in advance!

      P Offline
      P Offline
      Paramu1973
      wrote on last edited by
      #3

      Actually playing with culture info is not good. Using the Datetime control is better. Did u try? I used the below code. Its fine. Sub Form_Load() Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB", False) End Sub Thanks & Regards PARAMU:thumbsup:

      L 1 Reply Last reply
      0
      • A Andy_L_J

        Do you have a DateTimePicker in the DataGridViewCell or is it a default DataGridViewCell (Text) Field? You may need to convert the value to a valid Date Type.

        I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife

        L Offline
        L Offline
        LordZoster
        wrote on last edited by
        #4

        Hallo the DGV is a plain one, is bound to a BindingSource which has is DataSource property set to the DataTable. No modification is made on the fields type: the field holding the datetime is a default DataGridViewCell, automatically created at binding. You're giving me an interesting hint... By the way I hope that I should not create manually each column in the DataGridView to have it right typed .

        1 Reply Last reply
        0
        • P Paramu1973

          Actually playing with culture info is not good. Using the Datetime control is better. Did u try? I used the below code. Its fine. Sub Form_Load() Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB", False) End Sub Thanks & Regards PARAMU:thumbsup:

          L Offline
          L Offline
          LordZoster
          wrote on last edited by
          #5

          Hallo paramu, thank for the reply. The datatable.locale is used by Microsft in many examples, I kept it for preventing any issue from handling italian characters like accents, and for datetime handling too. I tried your code but it didn't work, neither with "en-GB" nor with "it-IT". It sounds that this is happening: 1. the default value in datatable is correct 2. the datagridview format it as string 3. when saving, the system fails to handle the string. I would like to try to explicitly type the datetime column, but this would require me to manually code each column of the DGV. Ubi maior, minor cessat... I'll give it a try.

          P 1 Reply Last reply
          0
          • L LordZoster

            Hallo paramu, thank for the reply. The datatable.locale is used by Microsft in many examples, I kept it for preventing any issue from handling italian characters like accents, and for datetime handling too. I tried your code but it didn't work, neither with "en-GB" nor with "it-IT". It sounds that this is happening: 1. the default value in datatable is correct 2. the datagridview format it as string 3. when saving, the system fails to handle the string. I would like to try to explicitly type the datetime column, but this would require me to manually code each column of the DGV. Ubi maior, minor cessat... I'll give it a try.

            P Offline
            P Offline
            Paramu1973
            wrote on last edited by
            #6

            While saving time, before to udate to your sever, it has to be Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US", False) And while save finish it has to be again Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB", False) Also form_load() Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB", False) Thanks & Regards PARAMU

            L 1 Reply Last reply
            0
            • P Paramu1973

              While saving time, before to udate to your sever, it has to be Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US", False) And while save finish it has to be again Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB", False) Also form_load() Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB", False) Thanks & Regards PARAMU

              L Offline
              L Offline
              LordZoster
              wrote on last edited by
              #7

              thanks it doesn't work. I'll try dgv.autogeneratecolumns = false and so on.

              L P 2 Replies Last reply
              0
              • L LordZoster

                thanks it doesn't work. I'll try dgv.autogeneratecolumns = false and so on.

                L Offline
                L Offline
                LordZoster
                wrote on last edited by
                #8

                I'm giving up, it's quite a nightmare if the system itself doesn't understand values it automatically wrote.

                A 1 Reply Last reply
                0
                • L LordZoster

                  thanks it doesn't work. I'll try dgv.autogeneratecolumns = false and so on.

                  P Offline
                  P Offline
                  Paramu1973
                  wrote on last edited by
                  #9

                  You mean, u need to convert it from string MyDataTable.Rows[RowNum]["MyDateTimeField"]=Convert.ToDateTime(MyStringDate); Are you talking about this?

                  L 1 Reply Last reply
                  0
                  • L LordZoster

                    I'm giving up, it's quite a nightmare if the system itself doesn't understand values it automatically wrote.

                    A Offline
                    A Offline
                    Andy_L_J
                    wrote on last edited by
                    #10

                    LordZoster wrote:

                    it's quite a nightmare if the system itself doesn't understand values it automatically wrote

                    This is why I nearly always roll my own DAL and handle the databinding with custom business objects. :| I never use the BindingSource stuff..

                    I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife

                    1 Reply Last reply
                    0
                    • P Paramu1973

                      You mean, u need to convert it from string MyDataTable.Rows[RowNum]["MyDateTimeField"]=Convert.ToDateTime(MyStringDate); Are you talking about this?

                      L Offline
                      L Offline
                      LordZoster
                      wrote on last edited by
                      #11

                      The BindingSource is supposed to handle this conversion by itself, doesn't it? And, in case I overrun this conversion writing my own one, where should I write it? From what I understand (very little, I admit) I should overrun a whole lot of stuff of BindingSource: in this I totally agree with Andy_J, it is preferrable to use one's own DAL. The worst thing is, that datetime cell is readonly, so there's no need to write it back into the database from the datagridview hence no need to convert any string...

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups