Introduction:

Here we will explain how to use GridView in asp.net or what is GridView in asp.net or how to bind data to the gridview in asp.net with example.

Description:

The Asp.Net GridView control displays data in a tabular format, and it also provide  sorting, paging, and editing functionality . It’s support Data binding using the DataSource property, which allows you to bind to various objects, including ADO.NET datasets or data table or System.Collections.ArrayList object

Example:

GridView.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView.aspx.cs" 
         Inherits="AspDotNet.GridView" %>
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="empgrid" runat="server"
     AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField ItemStyle-Width="150px" HeaderText="Employee ID">
            <ItemTemplate>
                <asp:Label ID="lblEmployeeId" runat="server"
                  Text='<%#Eval("EmployeeId")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width="300px" HeaderText="Epmloyee Name">
            <ItemTemplate>
                <asp:Label ID="lblEpmloyeeName" runat="server" Text='<%#
                         Eval("EpmloyeeName")%>'></asp:Label>
            </ItemTemplate>
            
        </asp:TemplateField>                                 <asp:TemplateField ItemStyle-Width="100px" HeaderText="Age">
            <ItemTemplate>
                 <asp:Label ID="lblAge" runat="server" Text='<%#
                         Eval("Age")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        
    </Columns>
     </asp:GridView>
    </div>
    </form>
</body>
</html>


GridView.aspx.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace AspDotNet
{
    public partial class GridView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ArrayList list = new ArrayList();
                Employee emp = new Employee();
                emp.EmployeeID = "EMPID0001";
                emp.EpmloyeeName = "Johen Mark";
                emp.Age = 25;
                list.Add(emp);
                emp = new Employee();
                emp.EmployeeID = "EMPID0002";
                emp.EpmloyeeName = "Mahesh Patel";
                emp.Age = 30;
                list.Add(emp);
                emp = new Employee();
                emp.EmployeeID = "EMPID0003";
                emp.EpmloyeeName = "Rohan Roy";
                emp.Age = 26;
                list.Add(emp);
                emp = new Employee();
                emp.EmployeeID = "EMPID0004";
                emp.EpmloyeeName = "Mohan singh";
                emp.Age = 28;
                list.Add(emp);
                emp = new Employee();
                emp.EmployeeID = "EMPID0005";
                emp.EpmloyeeName = "Avnish Kumar";
                emp.Age = 24;
                list.Add(emp);
                empgrid.DataSource = list;
                empgrid.DataBind();
            }
        }
        class Employee
        {
            public string EmployeeID { get; set; }          
            public string EpmloyeeName { get; set; }
            public int Age { get; set; }
        }
    }
}


When you run Above example code Output will be shown like below:


How to use Gridview control in asp.net


GridView Properties:

I Hope this Article Will Help to you.

Properties

Description

AllowPaging

Its enable to you paging feature.

AllowSorting

It’s enable to you sorting feature.

AutoGenerateColumns

The bound fields are automatically created for each field in the data source.

comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like