How to Display Two Columns in One DataGrid Column

Written by declan maher
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Display Two Columns in One DataGrid Column
Combine two columns into one in a datagrid by using SQL Editor. (Data image by Oleksiy Ilyashenko from Fotolia.com)

Displaying two columns from a database table as one column on a website is often needed. A typical example is where we want to show a person's first name and last name in one column in a datagrid. The easiest way of doing this is to create an SQL statement that builds a new display column containing both database columns.

Skill level:
Easy

Other People Are Reading

Things you need

  • SQL Editor

Show MoreHide

Instructions

  1. 1

    Open SQL Editor.

  2. 2

    Create an SQL statement to concatenate (or join) both database table columns and output as one column called "FullName":

    select FirstName +' '+LastName as FullName from users

  3. 3

    Use the variable "FullName" where you require it in the DataGrid:

    <asp:DataGrid runat="server" AutoGenerateColumns="False" ... >

    <Columns >

    &lt;asp:TemplateColumn HeaderText="Name" &gt;
    
      &lt;ItemTemplate &gt;
    
        &lt;%# Container.DataItem("FullName") %&gt;
    
      &lt;/ItemTemplate &gt;
    
    &lt;/asp:TemplateColumn &gt;
    

    </Columns>

    </asp:DataGrid >

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.