By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,119 Members | 2,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,119 IT Pros & Developers. It's quick & easy.

Merge two fields from DataTable into one?

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a
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

Nov 21 '05 #2

P: n/a
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
Nov 21 '05 #3

P: n/a
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

Nov 21 '05 #4

P: n/a
Ken,
dc.DataType = System.Type.GetType("System.String") 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***@bellsouth.net> wrote in message
news:eA**************@TK2MSFTNGP12.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("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

Nov 21 '05 #5

P: n/a
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*************@TK2MSFTNGP12.phx.gbl...
Ken,
dc.DataType = System.Type.GetType("System.String")

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***@bellsouth.net> wrote in message
news:eA**************@TK2MSFTNGP12.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("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


Nov 21 '05 #6

P: n/a
Jay,

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

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

Just to inform.

Cor

Nov 21 '05 #7

P: n/a
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****************@TK2MSFTNGP10.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*************@TK2MSFTNGP12.phx.gbl...
Ken,
dc.DataType = System.Type.GetType("System.String")

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***@bellsouth.net> wrote in message
news:eA**************@TK2MSFTNGP12.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("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



Nov 21 '05 #8

P: n/a
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.Compute)!

Hope this helps
Jay

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

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

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

Just to inform.

Cor

Nov 21 '05 #9

P: n/a
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***@bellsouth.net> wrote in message
news:u6**************@TK2MSFTNGP12.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****************@TK2MSFTNGP10.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*************@TK2MSFTNGP12.phx.gbl...
Ken,
dc.DataType = System.Type.GetType("System.String")

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***@bellsouth.net> wrote in message
news:eA**************@TK2MSFTNGP12.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("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



Nov 21 '05 #10

P: n/a
Jay,

As I wrote often before, this is not meant as any kind of negative critique.
I would not know why I should do that.

There was nothing wrong with your text except, that that "compute" catched
my eyes.

The word "compute" by the way on your showed pages is only used 1 time as a
minor text for the same sample on 2 pages, not in the description. The used
word "calculate" gives for me a better distinct from the vertical "compute".
Where I don't discuss the meaning in English from those words.

You are free to do yourself with what I wrote of course, it was just to help
to make it even better.

Cor

Nov 21 '05 #11

P: n/a
You know Cor this post & your previous post on Computer Columns really don't
warrant a response from me.

Jay

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

As I wrote often before, this is not meant as any kind of negative
critique.
I would not know why I should do that.

There was nothing wrong with your text except, that that "compute" catched
my eyes.

The word "compute" by the way on your showed pages is only used 1 time as
a minor text for the same sample on 2 pages, not in the description. The
used word "calculate" gives for me a better distinct from the vertical
"compute". Where I don't discuss the meaning in English from those words.

You are free to do yourself with what I wrote of course, it was just to
help to make it even better.

Cor

Nov 21 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.