如何正确连接数据表的列的值? [英] How to properly concatenate the values of Columns of a DataTable?
问题描述
在以下代码中,我注意到 A
和 B
列显示了数据.
但是,列 C
和 D
为空.为什么?
In the following code, I noticed that Columns A
and B
show data.
However, columns C
and D
are empty. Why?
DataTable _dt = new DataTable();
DataColumn A=_dt.Columns.Add("A");
DataColumn B=_dt.Columns.Add("B");
DataColumn C=_dt.Columns.Add("C",typeof(String),"A+B");
DataColumn D=_dt.Columns.Add("D",typeof(String),"A+'!'+B");
DataRow _dr=_dt.NewRow();
_dr["A"]="A";
_dr["B"]="B";
Console.WriteLine(_dr["A"]);
Console.WriteLine(_dr["B"]);
Console.WriteLine(_dr["C"]); //Shows blank-why? Expected AB
Console.WriteLine(_dr["D"]); //Shows blank-why? Expected A!B
推荐答案
Add the DataRow to the DataTable, that's when the Expression is first evaluated:
这些是计算列.当 +
符号与数据类型为String的Columns一起使用时,这些值将串联在一起.
These are calculated columns. When the +
symbols is used with Columns of data type String, the values are concatenated.
字符串运算符
T o连接一个字符串,使用+字符.的价值DataSet类的CaseSensitive属性确定字符串是否比较是区分大小写的.但是,您可以覆盖该值带有DataTable类的CaseSensitive属性.
DataRow _dr=_dt.NewRow();
_dr["A"]="A";
_dr["B"]="B";
_dt.Rows.Add(_dr);
请注意,以上四行可以写为:
As a note, the four lines above can be written as:
_dt.Rows.Add("A", "B");
结果当然是相同的.列 C
和 D
的值不包含在 object []
数组中,而是由列的表达式生成的.
The result is of course the same. The values of Columns C
and D
are not included in the object[]
array, but generated by the Columns' Expressions.
现在它应该输出:
A
B
AB
A!B
这篇关于如何正确连接数据表的列的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!