数据行添加值 [英] Datatable row add value

查看:86
本文介绍了数据行添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



有数据表列类型 - 十进制。是否可以将广告字符串添加到此列(行)。



string =名称;不是123.12。我尝试过十进制解析但没有工作。



提前谢谢

解决方案

答案是否答案A十进制变量不能存储字符串。 Decimal.Parse()将解析有效的十进制字符串,例如 - 123.12。

如果您需要存储此类值,请更改数据类型为字符串。您可以在其中存储任何文本,包括十进制(作为文本)。


无法将字符串放入数据表的十进制列中。

如果您想要改变以这种方式存储的内容,那么将数据表定义更改为 typeof(string)并使用.ToString存储任何数字()



示例:

 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());



产生输出

  1  2  
a



这是你在这里想要实现的目标的问题


问题是你不能只是将一个字符串添加到一个接受十进制值的列。为简单起见,为什么不改为将列类型更改为字符串。如果是小数,则该列将隐式将其转换为字符串。如果你输入一个字符串,它也会接受它。



我刚回答了一个类似的问题,我在DataTable中添加了两列并添加了两行。看看我的例子,我需要显示日期,但为了简单起见我保留了DataTable列类型字符串,因为我用来获取Date的变量是一个DateTime变量:数据表两行列 [ ^ ]

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

解决方案

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.


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

1.2
a


It''s a question of what are you actually trying to achieve here


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[^]


这篇关于数据行添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆