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

Combining 2 field into one column

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:6d****************************@phx.gbl...
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

Nov 19 '05 #2

P: n/a
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" wrote:
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" <an*******@discussions.microsoft.com> wrote in message
news:6d****************************@phx.gbl...
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


Nov 19 '05 #3

P: n/a
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 tocome!)
"Red" <an*******@discussions.microsoft.com> wrote in messagenews:6d****************************@phx.gbl...
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

.

Nov 19 '05 #4

P: n/a
Either a DataTable or Delirium Tremens.

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Red" <Re*@discussions.microsoft.com> wrote in message
news:6f****************************@phx.gbl...
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!)
"Red" <an*******@discussions.microsoft.com> wrote in

message
news:6d****************************@phx.gbl...
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

.

Nov 19 '05 #5

P: n/a
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!)
"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:eT*************@TK2MSFTNGP15.phx.gbl...
Either a DataTable or Delirium Tremens.

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Red" <Re*@discussions.microsoft.com> wrote in message
news:6f****************************@phx.gbl...
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!)
"Red" <an*******@discussions.microsoft.com> wrote in

message
news:6d****************************@phx.gbl...
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
.


Nov 19 '05 #6

P: n/a
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 provideany inital code, I took a guess that you were using datasets ordatatables....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 selectFirstName + ' ' + LastName as FullName, userId, ....

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup isannoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more tocome!)
"Juan T. Llibre" <no***********@nowhere.com> wrote in messagenews:eT*************@TK2MSFTNGP15.phx.gbl...
Either a DataTable or Delirium Tremens.

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Red" <Re*@discussions.microsoft.com> wrote in message
news:6f****************************@phx.gbl...
> 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!)
>>
>>
>>"Red" <an*******@discussions.microsoft.com> wrote in
> message
>>news:6d****************************@phx.gbl...
>>> 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
>>
>>
>>.
>>


.

Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.