473,657 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: changing the datatype of a column in a datatable

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
Nov 20 '05 #1
1 2228


--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/...ity/newsgroups
"Geoff Jones" <ge***@NODAMNSP AM.com> wrote in message
news:40******** *************@n ews.dial.pipex. com...
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/de...classtopic.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/de...ssionTopic.asp
and here's an example I wrote
http://www.knowdotnet.com/articles/dataviews1.html
HTH,

Bill
Geoff

Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
10909
by: Jon Brunson | last post by:
Is it possible to use a DataAdapter to fill a DataTable, change the DataColumns of that DataTable (and maybe even it's name) and then commit those changes to the database (in my case SQL Server 2000)? Eg: Dim dc As New SqlCommand("SELECT TOP 0 * FROM SomeTable", SomeSqlConnection)
2
45879
by: ad | last post by:
I use ado.net to fill a Excel wroksheet into a DataTable. The data in the Excel wroksheet is digital. After the data filled into the DataTable, the DataType of each column is set to Double, but I want to treat them as string, I use the code to covert them DataTable dt = ds.Tables0]; int iColCount = dt.Columns.Count; for (int j = 0; j < iColCount; j++)
1
1986
by: Rippo | last post by:
I am returning a datatable from a stored procedure and then iterating through the columns. I am trying to find out if a column has the sql data type of ntext. In .net the datatype of a sql ntext column is System.String. .Net also returns System.String for varchar, char, text, nvarchar and nchar. This obviously does not help me as I want to identify the ntext column I have searched for this and cannot find the answer in .net. I can...
2
12175
by: Griff | last post by:
When attempting to change the datatype for a datatable column with the following code: dtap.Columns(4).DataType = system.Type.GetType"System.String") I keep getting an error that says that I can't change the datatype because there is data in the table. However, I know there is no data in the table, as I have cleared the table and tested the results. Please help.
6
11256
by: Sam | last post by:
Hi Here is my code : Code: Dim dt As New DataTable dt.Columns.Add.ColumnName = "New" dt.Columns("New").DataType = System.Type.GetType("System.String") dt.Columns.Add.ColumnName = "Id" dt.Columns("Id").DataType = System.Type.GetType("System.Integer")
7
4561
by: Simon Verona | last post by:
I posted this in dotnet.languages.vb.controls but thought I'd post here as well.. I have a combobox that is bound to a dataview generated from a dataset. The dataset has a single table (called "Data") with two columns "Id" and "Description". Id contains a code and description contains the description that is displayed in the combobox.
2
2665
by: wapsiii | last post by:
How do I query the datatype of a column in a datatable (Ineed to find the smalldatetime datatypes in a ms sql produced datatable). I'm trying this, but is isn't quite right: For i As Byte = 0 To dt.Columns.Count If TypeOf dt.Columns(i) Is DateTime Then ' can't find it End If Next
4
3140
by: UJ | last post by:
I have a datatype that I want to use as a type for a column (It's an enumeration.) but I can't get the syntax right. Can you even do it? Or do I need to do it as a string and convert it back and forth? TIA - Jeff.
1
3562
by: keithb | last post by:
The following code sets a DataTable column DataType to "String" column.DataType = System.Type.GetType("System.String"); What is the corresponding statement when the data type is boolean? I have tried both "bit', "bool", and "boolean" and all cause an error. One would think that a simple search for "System.Type.GetType" in Books On Line would yield an answer but all I get is page after page after page of loosely unrelated...
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.