VB.NET: Issues With Changing The DataType Of A Column
I'm working with a DataGridView that gets its data from a server somewhere on the network in the form of an XML document. Therefore it's an unbound grid (*) for which some processing has to be done behind the scenes (XML >> DataTable >> Binding by code). Now I noticed an amount somewhere could be changed by the user ending up in decimals, instead of the required whole numbers. Thinking this could be changed easily I naively did
dt.Columns.Add("Amount")
dt.Columns("Amount").DataType=GetType(Int32)
resulting in a nice
ArgumentException
Cannot change DataType of a column once it has data.
The solution - of course - was to first build a DataColumn object with the right Type and bind that to the DataTable:
Dim dc1 As New DataColumn
dc1.DataType = GetType(Int32)
dc1.ColumnName = "Amount"
ds.Tables(0).Columns.Add(dc1)
Learned something new, hopefully it wil help you too.
(*) Naturally, would the grid have been bound, the DataType would folow automatically from the underlying DataSource...