Friday, July 21, 2017 Gridview : Merge two or more fields together

In this article I am going to explain how to merge two or more fields together (concatenate) in Gridview.
I want to show two or more fields together (concatenate). We can do this in Itemtemplate. It is not possible to concatenate fields in boundfiled. If want to show concatenate fields in Boundfield, then you have to concatenate fields in sql store procedure.

I have created a table users and insert some dummy records into it. Gridview : Merge two or more fields together Gridview : Merge two or more fields together

I want to concatenate First and last together.

HTML Markup of Webform:

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="grdusers" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <asp:TemplateField HeaderText="Name">
                     <asp:Label ID="Label1" runat="server" Text='<%# Eval("Firstname")+" "+Eval("Lastname") %>' ToolTip='<%# Eval("Firstname")+" "+Eval("Lastname") %>'></asp:Label>
                <asp:TemplateField HeaderText="Name">
                        <%# Eval("Firstname") %> <%# Eval("Lastname") %>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

Add namespace

C# Code:
using System.Configuration;
using System.Data.SqlClient;
using System.Data; Code:
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data

Bind Gridview
Create a method to bind gridview and call it on page load event.

C# Code:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
    private void BindGrid()
        SqlDataAdapter adp = new SqlDataAdapter("Select * from Users", con);
        DataTable dt = new DataTable();
        if (dt.Rows.Count > 0)
            grdusers.DataSource = dt;
    } Code:

   Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
        End If
    End Sub
    Private Sub BindGrid()
        Dim adp As New SqlDataAdapter("Select * from Users", con)
        Dim dt As New DataTable()
        If dt.Rows.Count > 0 Then
            grdusers.DataSource = dt
        End If
    End Sub

 Results: Gridview : Merge two or more fields together

No comments:

Post a Comment