Combining 2 field into one column

  • Thread starter Thread starter Red
  • Start date Start date
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
 
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
 
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
 
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


.
 
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 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


.
 
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 said:
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 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


.
 
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.



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 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


.


.
 
Back
Top