Wednesday 10 February 2016

Display multiple data Fields (Columns) in GridView BoundField in ASP.Net using C#

Bind Multiple DataField in BoundField of Gridview
Description:-


In this example we explain that how to display multiple column in GridView Bounfield in asp.net.or how to display multiple data fields in Gridview in asp.net.

As we all know that by default the BoundField column of gridview does not allow to bind multiple coloum.so to overcome from this we must have to use RowDataBound event of the Gridview and in which we can displayed multiple column (Data Field) in BoundField of Gridview.

BindMultipleColomnInGridview.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BindMultipleColomnInGridView.aspx.cs"
    Inherits="WebApplication1_BindMultipleColomnInGridView" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <style type="text/css">
        .GridviewDiv
        {
            font-size: 100%;
            font-family: 'Lucida Grande' , 'Lucida Sans Unicode' , Verdana, Arial, Helevetica, sans-serif;
            color: #303933;
        }
        .headerstyle
        {
            color: #FFFFFF;
            border-right-color: #abb079;
            border-bottom-color: #abb079;
            background-color: #df5015;
            padding: 0.5em 0.5em 0.5em 0.5em;
            text-align: center;
        }
    </style>
    <title>
Display multiple data Fields (Columns) in GridView BoundField in ASP.Net </title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView runat="server" ID="grdEmployee" AutoGenerateColumns="false" OnRowDataBound="grdEmployee_RowDataBound">
        <HeaderStyle CssClass="headerstyle" />
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Emp Id" ItemStyle-Width="90" />
            <asp:BoundField DataField="" HeaderText="Name" ItemStyle-Width="120" />
            <asp:BoundField DataField="Salary" HeaderText="Salary" ItemStyle-Width="100" />
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>

 BindMultipleColomnInGridview.aspx.cs:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class WebApplication1_BindMultipleColomnInGridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id"), new DataColumn("FirstName"), new DataColumn("LastName"), new DataColumn("Salary") });
            dt.Rows.Add(1, "Kirit", "Patel", 25000);
            dt.Rows.Add(2, "Pintu", "Patel", 40000);
            dt.Rows.Add(3, "Raju", "Maheta", 30000);
            grdEmployee.DataSource = dt;
            grdEmployee.DataBind();
        }
    }

    protected void grdEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Text = string.Format("{0} {1}", DataBinder.Eval(e.Row.DataItem, "FirstName"), DataBinder.Eval(e.Row.DataItem, "LastName"));
        }
    }
}


0 comments:

Post a Comment