locked
DataGridView DateTime bound cell, delete contents, yields DataFormat error RRS feed

  • Question

  • Hi!

    I have a class with a couple of properties. Among them is property declared as follows:

    public DateTime? fraFollowupDate { get; set; }

    When I populate the class from the SQL server database the Property is set to null as it should be. Displaying the data in the datagridview is also ok, no value is shown.

    If i enter a date in the cell, the date is stored in the property, how ever if I delete the date from the cell, I want the property to be set to null again. Now i get an error "FormatExeption: String was not recognized as a valid DateTime...."

    I know this is beacuse i deleted the cell contents, but i want to be able to have no cell contents, is it possible?

    Any hints or directons to simular solutions are greatly appreciated.

    With best regards
    Martin Arvidssson, Data Martin i Kungsbacka AB

    Wednesday, July 2, 2014 10:42 AM

Answers

  • Solved it, using CellParsing event, there I set Null if the string is empty.
    • Marked as answer by Datamartin Wednesday, July 9, 2014 10:07 AM
    Wednesday, July 9, 2014 10:07 AM

All replies

  • Hi Datamartin,

    May I know how you polulate the class and how you bind the class to the DataGridView control? Could you please show us your code, and tell us which line gives this exception?

    To save a cell value back to the nullable DateTime? property, I think you could change the empty string to null, then save null value to the nullable property. For example in the CellEndEdit event handler of the DataGridview, check if the cell value is an empty string, if it is, then set the property value as null.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Caillen Thursday, July 3, 2014 7:40 AM
    Thursday, July 3, 2014 6:21 AM
  • Hi!

    The class looks like this:

    public class FamilyRequestAdoptionCountries
        {
            [NoInsert]
            public int fraID { get; set; }
    
            [NoInsert, NoUpdate]
            public int fraFamilyID { get; set; }
    
            [NoInsert, NoUpdate]
            public int? fraFileNumber { get; set; }
    
            [NoInsert, NoUpdate]
            public string fraCountry { get; set; }
    
            public bool? fraWishForCountry { get; set; }
    
            public bool? fraDoesNotFitForCountry { get; set; }
    
            public bool? fraFitsForCountry { get; set; }
    
            public string fraNotes { get; set; }
    
            public DateTime? fraFollowupDate { get; set; }
    
            [NoInsert, NoUpdate]
            public bool? fraArchive { get; set; }
    
            [NoInsert, NoUpdate]
            public DateTime fraArchiveDate { get; set; }
    
            [NoInsert, NoUpdate]
            public string fraLastModifyedBy { get; set; }
    
            /// <summary>
            /// True, om posten modifierats
            /// </summary>
            public bool Modifyed = false;
        }
    
        public class FamilyRequestAdoptionCountriesList
        {
            public List<FamilyRequestAdoptionCountries> _FamilyRequestAdoptionCountries = new List<FamilyRequestAdoptionCountries>();
            public List<FamilyRequestAdoptionCountries> FamilyRequestedAdoptionCountries { get { return _FamilyRequestAdoptionCountries; } set { _FamilyRequestAdoptionCountries = value; } }
        }

    The binding is done like this:

     DataSet dsRequestAdoptionCountries;
    
                dsRequestAdoptionCountries = DataLogic.FamilyLL.GetFamilyRequestAdoptionCountries(familyMemberNo);
                requestAdoptionCountries = new DataLogic.DEOFamilies.FamilyRequestAdoptionCountriesList();
                requestAdoptionCountries._FamilyRequestAdoptionCountries = DataLogic.DataHelper.DataSetToListDEO<DataLogic.DEOFamilies.FamilyRequestAdoptionCountries>(dsRequestAdoptionCountries.Tables[0]);
    
                familyRequestedAdoptionCountriesBindingSource.DataSource = requestAdoptionCountries.FamilyRequestedAdoptionCountries;

    I did try to modify the data in the CellEndEdit event but it didn't work. I think that the error is generated from the DataGridView class itself.

    When I debug the code and look at the grid.Rows[e.RowIndex].Cells[fraFollowupDate].value, I can see that the object is infact DateTime.

    The problem is that customer needs to blank this date out and when I do this, store null in the SQL database.

    Hope this information helps, don't hasitate to ask me for more code.

    Regards
    Martin

    Thursday, July 3, 2014 12:15 PM
  • Solved it, using CellParsing event, there I set Null if the string is empty.
    • Marked as answer by Datamartin Wednesday, July 9, 2014 10:07 AM
    Wednesday, July 9, 2014 10:07 AM