Click here to Skip to main content
15,914,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

There are datatable column typeof - decimal.Is it possible to ad string to this column(row).

string = "Name"; not "123.12". I tried decimal parse but didnt work.

Thanks in advance
Posted
Comments
Herman<T>.Instance 6-Feb-13 8:11am    
can you explain more precise what your problem is?

The answer is No. A decimal variable cannot store a string. Decimal.Parse() would parse a valid decimal string, for ex - "123.12".
If you need to store such value, change the data type to string. You can store any text including decimal (as text) in that.
 
Share this answer
 
v2
Comments
CHill60 6-Feb-13 8:19am    
Apologies - didn't see your response as I was posting mine - that's twice I've done that today!
Ankur\m/ 6-Feb-13 8:25am    
It's absolutely alright. There are high possibilities of that happening. Happens with me as well. :)
It is not possible to put strings into decimal columns on a datatable.
If you really want to vary what is stored in this way then change the datatable definition to typeof(string) and store any numbers using .ToString()

Example:
DataTable dt = new DataTable();
dt.Columns.Add("col1", typeof(string));
dt.Rows.Add((1.2).ToString());
Debug.Print(dt.Rows[0]["col1"].ToString());
dt.Rows.Add("a");
Debug.Print(dt.Rows[1]["col1"].ToString());

produces the output
VB
1.2
a

It's a question of what are you actually trying to achieve here
 
Share this answer
 
v2
The thing is you can't just add a string to a column that was made to accept decimal values. Why don't you instead change the column type to string for simplicity. If it is a decimal, then the column will implicitly convert it to string. If you enter a string it will accept it as well.

I just answered a similar question where I added two columns in a DataTable and added two rows. Look at my example, I needed to display date but I kept the DataTable column type string for simplicity where as the variable I used to get the Date was a DateTime variable: Datatable two line columns[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900