Merge two fields from DataTable into one?

  • Thread starter Thread starter Bruce D
  • Start date Start date
B

Bruce D

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

--------------------
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/d.../frlrfsystemdatadatacolumnclassctortopic4.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/d...ormsdatagridcolumnstyleclassreadonlytopic.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
 
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
 
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,

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
 
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
----------------
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
 
Cor,
Huh?

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

http://msdn.microsoft.com/library/d.../frlrfSystemDataDataColumnClassctorTopic4.asp

http://msdn.microsoft.com/library/d.../frlrfsystemdatadatacolumnclassctortopic5.asp

http://msdn.microsoft.com/library/d...fSystemDataDataColumnClassExpressionTopic.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
 
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/default.asp?url=/library/en-us/vbls7/html/vblrfVBSpec911_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
 
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
 
You know Cor this post & your previous post on Computer Columns really don't
warrant a response from me.

Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top