none
ColumnChanging event will not fire RRS feed

  • Question

  • This should be a simple question to answer.

    I am using a streamreader to parse csv data into an array with the following line of code.

    FieldValues = csvStream.ReadLine.Split(",")
    

    FieldValues(10) contains the value "No" which needs to be converted to Boolean and written to a Boolean field in a datatable (dt).  I setup the column changing event to handle this but an InvalidCastException occurs before this column changing event code can run.

    The code below attempts to assign the value "No" to the Boolean field Retest, generates the exception which hijacks the ColumnChanging event.

    .Retest = FieldValues(10)
    

    Do I need to convert FieldValues(10) to Boolean before writing it to the datatable?  This seems to defeat the purpose of the ColumnChanging event.  I would really like to use the columnchanging event to convert this string to a boolean because the columnchanging event is firing for the many other columns I am adding to the datatable.

    Let me know if you need anymore details.

    John

    Sunday, May 15, 2011 12:59 PM

Answers

  • As you suspected, you should convert value to appropriate type before assigning the value. ColumnChanging isn't intended for this purpose but rather to take some action before the value is written to the DataTable.
    Miha Markic [MVP C#] http://blog.rthand.com
    Monday, May 16, 2011 6:01 AM

All replies

  • As you suspected, you should convert value to appropriate type before assigning the value. ColumnChanging isn't intended for this purpose but rather to take some action before the value is written to the DataTable.
    Miha Markic [MVP C#] http://blog.rthand.com
    Monday, May 16, 2011 6:01 AM
  • Hey,

    I agree with Miha. If you know the value of the specified index of an array is Boolean, you should use some simple .Net convertion. If you don't know, then you can use reflection to find the particular type and then convert.

    -Mohammed Ghouse Ibne Barq Kadapavi
    http://www.ProgrammersVision.blogspot.com
    https://sites.google.com/site/BarqKadapavi
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.


    Mohammed Ghouse Ibne Barq Kadapavi
    Monday, May 16, 2011 6:12 AM
  • Also note that Retest in your case is a property with its own type, thus you can't assign a string to a bool (the Retest property's type).
    Miha Markic [MVP C#] http://blog.rthand.com
    Monday, May 16, 2011 6:14 AM