Q: changing the datatype of a column in a datatable

  • Thread starter Thread starter Geoff Jones
  • Start date Start date
G

Geoff Jones

Hi

Is it possible to change the type of data a column holds in DataTable at
runtime? For example, suppose that the table column originally holds
Strings, can we, at runtime, change it to Integers?

I'm also interested in changing the date/time that may be held in a column.
That is, if the date and time are held in a column of DataTable, how can we
change it so it only has the time?

Geoff
 
--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
Geoff Jones said:
Hi

Is it possible to change the type of data a column holds in DataTable at
runtime?
Yes, just change the DataType property
http://msdn.microsoft.com/library/d.../html/frlrfsystemdatadatacolumnclasstopic.asp
However, if anything is in the column that can't be converted, you'll raise
an exception
For example, suppose that the table column originally holds
Strings, can we, at runtime, change it to Integers?

I'm also interested in changing the date/time that may be held in a column.
That is, if the date and time are held in a column of DataTable, how can we
change it so it only has the time?
For this I'd create an expression column, add it to the datatable and use
it. There is not a Time type per se although there is a Timespan but I
doubt that's what you want. You could use a string and format it with the
Expression and that would probably work. If you check the link I posted
above you'll see the types that are supported.

Here's a link to expression syntax
http://msdn.microsoft.com/library/d...fSystemDataDataColumnClassExpressionTopic.asp
and here's an example I wrote
http://www.knowdotnet.com/articles/dataviews1.html


HTH,

Bill
 
Back
Top