Combining 2 field into one column

R

Red

Hi all,

I would like to ask how to combine 2 field into one
column.
For example I have field first name and last name. When I
show it to the datagrid I want to show it as one column,
for example first name: Jack, lastname: sun become Jack
sun.

Thanks
 
K

Karl Seguin

you could add a new computed column to your dataset/datagrid:

dt.Columns.Add(new DataColumn("Denormalized",
System.Type.GetType("System.String"), "[FirstName] [LastName]"));

Karl
 
G

Guest

If you're not dynamically building your datagrid, your ItemTemplate might
look like this;

<ItemTemplate>
<asp:Label runat="server" Text='<%# string.Format( "{0}, {1}",
DataBinder.Eval(Container, "DataItem.LastName"),
DataBinder.Eval(Container, "DataItem.FirstName") ) %>' ID="Label1">
</asp:Label>
</ItemTemplate>

If you need to do something more complex (than string.Format), create a
protected method in the codebehind and call that. For example;

<ItemTemplate>
<asp:Label runat="server" Text='<%# FormatName(
(string)DataBinder.Eval(Container, "DataItem.LastName"),
(string)DataBinder.Eval(Container, "DataItem.FirstName") ) %>'
ID="Label1">
</asp:Label>
</ItemTemplate>

and in the codebehind;

protected string FormatName( string last, string first )
{
return string.Format( "{0}, {1}", last, first );
}

Karl Seguin said:
you could add a new computed column to your dataset/datagrid:

dt.Columns.Add(new DataColumn("Denormalized",
System.Type.GetType("System.String"), "[FirstName] [LastName]"));

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)


Red said:
Hi all,

I would like to ask how to combine 2 field into one
column.
For example I have field first name and last name. When I
show it to the datagrid I want to show it as one column,
for example first name: Jack, lastname: sun become Jack
sun.

Thanks
 
R

Red

Hello Karl,
How to add or where to add that code. another thing is
what is dt?

Thanks
-----Original Message-----
you could add a new computed column to your dataset/datagrid:

dt.Columns.Add(new DataColumn("Denormalized",
System.Type.GetType("System.String"), "[FirstName] [LastName]"));

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)


Hi all,

I would like to ask how to combine 2 field into one
column.
For example I have field first name and last name. When I
show it to the datagrid I want to show it as one column,
for example first name: Jack, lastname: sun become Jack
sun.

Thanks


.
 
J

Juan T. Llibre

Either a DataTable or Delirium Tremens.





Red said:
Hello Karl,
How to add or where to add that code. another thing is
what is dt?

Thanks
-----Original Message-----
you could add a new computed column to your dataset/datagrid:

dt.Columns.Add(new DataColumn("Denormalized",
System.Type.GetType("System.String"), "[FirstName] [LastName]"));

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)


Hi all,

I would like to ask how to combine 2 field into one
column.
For example I have field first name and last name. When I
show it to the datagrid I want to show it as one column,
for example first name: Jack, lastname: sun become Jack
sun.

Thanks


.
 
R

Red

Hi Karl
Thanks for ur code, its work.
Im using statement select
FirstName + ' ' + LastName as FullName, userId, .... Thanks one again
-----Original Message-----
Hahah...

dt was the DataTable you had all your data in....since you didn't provide
any inital code, I took a guess that you were using datasets or
datatables....if it's a dataset, it would simply be

dim dt as DataTable = ds.tables(0) or whatever table you wanted..

if you were using DataReader, i'd do it in the select statement select
FirstName + ' ' + LastName as FullName, userId, ....

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Either a DataTable or Delirium Tremens.





Red said:
Hello Karl,
How to add or where to add that code. another thing is
what is dt?

Thanks
-----Original Message-----
you could add a new computed column to your
dataset/datagrid:

dt.Columns.Add(new DataColumn("Denormalized",
System.Type.GetType("System.String"), "[FirstName]
[LastName]"));

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the
popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial
newsgroup FAQ (more to
come!)


message
Hi all,

I would like to ask how to combine 2 field into one
column.
For example I have field first name and last name.
When I
show it to the datagrid I want to show it as one
column,
for example first name: Jack, lastname: sun become Jack
sun.

Thanks


.


.
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top