且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将DataTable中的两个字段合并为一个字段?

更新时间:2022-12-12 13:45:43




在数据集中添加一个新的计算列。在列表框中使用该列作为

显示成员。

Dim dc As DataColumn


''


''在客户表中添加一个新列


''是全名。


dc = New DataColumn(" Name")

dc.DataType = System.Type.GetType(" System.String")


dc.Expression =" LastName +'',''+ FirstName"


dsClient.Tables(0).Columns.Add(dc)


Ken


--------------------

" Bruce D" &LT峰; br ************* @ hotmail.com>在消息中写道

新闻:11 ************* @ corp.supernews.com ...

我有一个DataTable我需要合并''lastname''和''firstname''

字段以显示在数据网格中。我知道我可以使用SQL语句将
合并为一个字段,但我想知道是否可以通过

将数据表,DataView或我已创建的DataGrid显示

数据。

这应该很简单......对吧?


-bruce duncan


Bruce,


您可以通过向数据表添加额外的列并使用表达式

在构造函数中。

http://msdn.microsoft.com/library/de...ctortopic4.asp


并且比datatable.acceptchanges更好地将行状态恢复为不变

(这是你填充数据表之后的直接状态)


但是你只能使用它来显示它,并且可能直接从它产生
你的风格中的一个只读字段(我从来没有这样用

数据网格,所以你应该尝试)。

http://msdn.microsoft.com/library /de...donlytopic.asp

我希望这有帮助吗?


Cor


Bruce,

您是否尝试过将计算列添加到DataTable中?


假设您有一个firstname和姓氏您的DataTable中的列,您可以定义全名和全名。计算列包含:


Dim table As DataTable

table.Columns.Add(" fullname",GetType(String)," lastname +'',''+

firstname")


有关表达式中允许的内容的详细信息,请参阅

DataColumn.Expression帮助主题。


希望这有帮助

Jay


" Bruce D" &LT峰; br ************* @ hotmail.com>在消息中写道

新闻:11 ************* @ corp.supernews.com ...
我有一个DataTable我需要合并要在数据网格中显示的''lastname''和'firstname''
字段。我知道我可以使用SQL语句
将字段合并为一个,但我想知道是否可以通过DataTable,DataView或我已经创建的DataGrid来显示数据。
这应该很简单......对吧?

-bruce duncan

>


I have a DataTable that I need to merge the ''lastname'' and ''firstname''
fields to be displayed in a datagrid. I know I can use the SQL statement to
merge the fields into one, but I want to know if it''s possible to do through
the DataTable, DataView or DataGrid that I have already created to display
the data.
This should be easy...right?

-bruce duncan

Hi,

Add a new calculated column to your dataset. Use that column as the
displaymember in the listbox.

Dim dc As DataColumn

''

'' Add a new column in clients table which

'' is full name.

dc = New DataColumn("Name")

dc.DataType = System.Type.GetType("System.String")

dc.Expression = "LastName + '', '' + FirstName"

dsClient.Tables(0).Columns.Add(dc)

Ken

--------------------
"Bruce D" <br*************@hotmail.com> wrote in message
news:11*************@corp.supernews.com...
I have a DataTable that I need to merge the ''lastname'' and ''firstname''
fields to be displayed in a datagrid. I know I can use the SQL statement to
merge the fields into one, but I want to know if it''s possible to do through
the DataTable, DataView or DataGrid that I have already created to display
the data.
This should be easy...right?

-bruce duncan


Bruce,

You can by adding an extra column to your datastable and use an expression
in the constructor of that.

http://msdn.microsoft.com/library/de...ctortopic4.asp

And do than a datatable.acceptchanges to set the rowstates back to unchanged
(This is all direct after that you filled your datatable)

However you can only use this to display and should make probably directly
from it a readonly field in your styles (I never did it this way with a
datagrid, so that should you try).

http://msdn.microsoft.com/library/de...donlytopic.asp

I hope this helps?

Cor


Bruce,
Have you tried adding a computed column to your DataTable?

Assuming you have a "firstname" and "lastname" column in your DataTable, you
can define a "fullname" computed column with:

Dim table As DataTable

table.Columns.Add("fullname", GetType(String), "lastname + '', '' +
firstname")

For details on what is allowed in the expression, see the
DataColumn.Expression help topic.

Hope this helps
Jay

"Bruce D" <br*************@hotmail.com> wrote in message
news:11*************@corp.supernews.com...
I have a DataTable that I need to merge the ''lastname'' and ''firstname''
fields to be displayed in a datagrid. I know I can use the SQL statement
to
merge the fields into one, but I want to know if it''s possible to do
through
the DataTable, DataView or DataGrid that I have already created to display
the data.
This should be easy...right?

-bruce duncan