Results 1 to 9 of 9

Thread: adding date to grid columns

  1. #1
    Member
    Join Date
    Jul 08
    Posts
    57

    adding date to grid columns

    i jst want to bind a grid which has date column
    & the user shld be able to add date in each row of the date column
    if no date is added the default date of 01/01/1900 shld be saved in db

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,748

  3. #3
    Member
    Join Date
    Jul 08
    Posts
    57

    Re: adding date to grid columns

    the link provides abt adding calendar to column
    but if i need to manually add date to coulmn without date calendare?

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,748

    Re: adding date to grid columns

    You don't have to do anything specific to display a date value in a DataGridView. You can display a DateTime value in a DataGridView in exactly the same way as you display a String in a DataGridView. If you can do one, you can do the other.

  5. #5
    Member
    Join Date
    Jul 08
    Posts
    57

    Re: adding date to grid columns

    i am not able to do it as i get error
    the fIeld on db is smalldATETIME
    the error i get even if i focus on the grid is

    The following exception occurred in the DataGridView:



    System.FormatException: String was not recognized as a valid DateTime. ---> System.FormatException: String was not recognized as a valid DateTime.

    at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)

    at System.DateTime.Parse(String s, IFormatProvider provider)

    --- End of inner exception stack trace ---

    at System.Windows.Forms.Formatter.InvokeStringParseMe thod(Object value, Type targetType, IFormatProvider formatInfo)

    at System.Windows.Forms.Formatter.ParseObjectInternal (Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue)

    at System.Windows.Forms.Formatter.ParseObject(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)

    at System.Windows.Forms.DataGridViewCell.ParseFormatt edValueInternal(Type valueType, Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)

    at System.Windows.Forms.DataGridViewCell.ParseFormatt edValue(Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)

    at System.Windows.Forms.DataGridView.PushFormattedVal ue(DataGridViewCell& dataGridViewCurrentCell, Object formattedValue, Exception& exception)



    To replace this default dialog please handle the DataError event.
    ---------------------------
    OK
    ---------------------------

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,748

    Re: adding date to grid columns

    If the error message say that the String you entered was not recognised as a valid DateTime then the obvious solution is to enter a String that is recognised as a valid DateTime. That is exactly why you should use a DateTimePicker in each cell rather than a TextBox: so that the user can't enter an invalid value. If you are going to use a text box column then you will need to provide code that will handle the situation where the user enters invalid data and the error message tells you how to do that. To enter valid data you need to use a valid date/time format for the current system, which are defined by the regional settings on the machine, or else set the Format of the DefaultCellStyle for that column to the exact format string you want to use.

    Having said all that, exactly what value was it was the issue and exactly how was that value entered? Was it something you entered yourself or was it a blank value maybe?

  7. #7
    Member
    Join Date
    Jul 08
    Posts
    57

    Re: adding date to grid columns

    i tried to enter value in the column as 05/05/2012
    which is a valid date

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,748

    Re: adding date to grid columns

    It may look like a valid date to you but it is not considered a valid date format by the system.

  9. #9
    Member
    Join Date
    Jul 08
    Posts
    57

    Re: adding date to grid columns

    i think there is a lot of confusion as whtever i try i am not getting he end result
    frm all above thread i came to a conclusion tht using datepicker is the best way
    i m trying it ifant issue will get back

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •