473,587 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Merge two fields from DataTable into one?

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
Nov 21 '05 #1
11 19709
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("Nam e")

dc.DataType = System.Type.Get Type("System.St ring")

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

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

Ken

--------------------
"Bruce D" <br************ *@hotmail.com> wrote in message
news:11******** *****@corp.supe rnews.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

Nov 21 '05 #2
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.accep tchanges 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
Nov 21 '05 #3
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.A dd("fullname", GetType(String) , "lastname + ', ' +
firstname")

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

Hope this helps
Jay

"Bruce D" <br************ *@hotmail.com> wrote in message
news:11******** *****@corp.supe rnews.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

Nov 21 '05 #4
Ken,
dc.DataType = System.Type.Get Type("System.St ring") Rather then risk a runtime error with a bad string name on Type.GetType, I
normally use the GetType function:

dc.DataType = GetType(System. String)

This way if there was a typo in the type name, then I receive a compile
error, rather then a runtime error.

Otherwise the results are the same...

Just a thought
Jay

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:eA******** ******@TK2MSFTN GP12.phx.gbl... 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("Nam e")

dc.DataType = System.Type.Get Type("System.St ring")

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

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

Ken

--------------------
"Bruce D" <br************ *@hotmail.com> wrote in message
news:11******** *****@corp.supe rnews.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

Nov 21 '05 #5
I should add that the GetType() function/keyword also supports intellisense,
so you can "pick" the desired type.

I will however use Type.GetType when I am reading the type name from the
app.config or other config file...

Just a thought
Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:em******** *****@TK2MSFTNG P12.phx.gbl...
Ken,
dc.DataType = System.Type.Get Type("System.St ring")

Rather then risk a runtime error with a bad string name on Type.GetType, I
normally use the GetType function:

dc.DataType = GetType(System. String)

This way if there was a typo in the type name, then I receive a compile
error, rather then a runtime error.

Otherwise the results are the same...

Just a thought
Jay

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:eA******** ******@TK2MSFTN GP12.phx.gbl...
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("Nam e")

dc.DataType = System.Type.Get Type("System.St ring")

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

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

Ken

--------------------
"Bruce D" <br************ *@hotmail.com> wrote in message
news:11******** *****@corp.supe rnews.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


Nov 21 '05 #6
Jay,

Just too your attentition, I have seen that this gave once by somebody
misunderstandin gs.
Have you tried adding a computed column to your DataTable?

It was mixed up with DataTable.Compu te. As you know are it not only English
speaking people who visit this newsgroup.

Just to inform.

Cor

Nov 21 '05 #7
Hi,

You are right Type.GetType would be a better choice. Suprised you
get a compile time error. Copied this code from a working project.

Ken
----------------
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
I should add that the GetType() function/keyword also supports intellisense,
so you can "pick" the desired type.

I will however use Type.GetType when I am reading the type name from the
app.config or other config file...

Just a thought
Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:em******** *****@TK2MSFTNG P12.phx.gbl...
Ken,
dc.DataType = System.Type.Get Type("System.St ring")

Rather then risk a runtime error with a bad string name on Type.GetType, I
normally use the GetType function:

dc.DataType = GetType(System. String)

This way if there was a typo in the type name, then I receive a compile
error, rather then a runtime error.

Otherwise the results are the same...

Just a thought
Jay

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:eA******** ******@TK2MSFTN GP12.phx.gbl...
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("Nam e")

dc.DataType = System.Type.Get Type("System.St ring")

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

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

Ken

--------------------
"Bruce D" <br************ *@hotmail.com> wrote in message
news:11******** *****@corp.supe rnews.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



Nov 21 '05 #8
Cor,
Huh?

"Computed column" & "calculated column" are terms MSDN uses:

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

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

http://msdn.microsoft.com/library/de...ssionTopic.asp
Don't you agree that it is better to use the nomenclature that MS uses in
its books & on MSDN, rather then making up my own words?

Also, the example code itself rather clear that I was refering to a new
column (table.Columns. Add) rather then calling a method (DataTable.Comp ute)!

Hope this helps
Jay

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:uA******** ******@TK2MSFTN GP09.phx.gbl...
Jay,

Just too your attentition, I have seen that this gave once by somebody
misunderstandin gs.
Have you tried adding a computed column to your DataTable?

It was mixed up with DataTable.Compu te. As you know are it not only
English speaking people who visit this newsgroup.

Just to inform.

Cor

Nov 21 '05 #9
Ken,
You are right Type.GetType would be a better choice. Err...

Actually I was suggesting that a GetType expression

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

would be a better choice over Type.GetType.
Your code does not have a compile time error! I was stating if you
miss-typed the type name on a GetType expression you will receive a compile
error, however if you miss type the type name on the parameter to
Type.GetType you will receive a runtime error instead of a compile time
error... As the GetType expression expects an Identifier, where as
Type.GetType expects a String.

Hope this helps
Jay

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:u6******** ******@TK2MSFTN GP12.phx.gbl... Hi,

You are right Type.GetType would be a better choice. Suprised you
get a compile time error. Copied this code from a working project.

Ken
----------------
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
I should add that the GetType() function/keyword also supports
intellisense,
so you can "pick" the desired type.

I will however use Type.GetType when I am reading the type name from the
app.config or other config file...

Just a thought
Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:em******** *****@TK2MSFTNG P12.phx.gbl...
Ken,
dc.DataType = System.Type.Get Type("System.St ring")

Rather then risk a runtime error with a bad string name on Type.GetType,
I
normally use the GetType function:

dc.DataType = GetType(System. String)

This way if there was a typo in the type name, then I receive a compile
error, rather then a runtime error.

Otherwise the results are the same...

Just a thought
Jay

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:eA******** ******@TK2MSFTN GP12.phx.gbl...
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("Nam e")

dc.DataType = System.Type.Get Type("System.St ring")

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

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

Ken

--------------------
"Bruce D" <br************ *@hotmail.com> wrote in message
news:11******** *****@corp.supe rnews.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



Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1580
by: beerman | last post by:
I have developed an app (in Access 2002) that dynamically creates a table, and the fields in that table, for propagation based on the max number of occurences in a reference table. Basically, it runs all the necessary selects to produce a 'flat file' that is used with the mail merge (in Word 2002). My dilemma is, that the number of...
1
1543
by: Joey Lee | last post by:
Hi, I am confused as what the DataSet Merge() function does. Currently I have two datatable which was selected from different table in mssql. Now with both different datatable. there are unique keys which determine the the relationship.
1
1422
by: Chris Ashley | last post by:
Hi there, If i have a dataset which has a datatable which has fields matching an existing SQL Server DB Table exactly, is there an easy way to merge all the records from the datatable into my SQL table without having to write a stored procedure or SQL insert statement? Cheers, Chris
2
2102
by: muntyanu | last post by:
Hi all, I have problem when merging existing DataTable into new dataset. DataSet ds = new DataSet(); while ( done ) { // fill myCustomDataSet.MyTable with data ds.Merge( myCustomDataSet.MyTable, bPreserveChanges, MissingSchemaAction.Add ); ds.AcceptChanges(); // tried with and without this line
0
1033
by: Allen | last post by:
We converted our app from 1.1 to 2 and some code was no longer working. The case: From a non typed datatable, convert it to a typed one. How to reproduce the bug: ------------------------------- 1) Create a project. 2) Add a new Dataset (keep DataSet1). a) Add a new DataTable (Keep DataTable1) b) Add a new DataColumn (Keep DataColumn1)...
3
2602
by: John Cosmas | last post by:
I have a DATATABLE which I have populated in my application, and I need it written out to a particular table I specify in my ACCESS database. My code works to the point of the MERGE and UPDATE, but it creates exactly the number of BLANK records per the populated DATATABLE. Here is my code... pstrDestinationTable = "tws_tbl_Case_Scanner_" &...
3
3400
by: Matt F | last post by:
I'm having difficulty with a function and can't find what the problem is. The code is below. Essentially, it works fine if the target table (tbl variable in code) is empty, however if it contains rows prior to the method being called, the error "'column' argument cannot be null" is thrown on the tbl.merge line. Any help would be greatly...
1
15767
by: mj2736 | last post by:
I'm a little confused about DataTable.Merge(). I have two DataTable objects with the same structure - dtOrig and dtCurrent. The end result I'm trying to achieve is to get all the rows in dtCurrent that are in some way different from the corresponding rows in dtOrig so those new/ modified/deleted rows can be processed. My idea was to use...
1
6158
by: joproulx | last post by:
Hello all, Here is my problem: I am trying to merge 2 datasets but I don't want to overwrite rows that are already modified in my working dataset. Example: I have one Dataset with only one DataTable in it. The DataTable has these 2 columns: Column #1: Name="Id" Type=Int32 (Primary Key)
0
7918
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.