ASP.NET GridView


The GridView control is used to display the data in the tabular format, accept, display, and add, delete, update, sorting, paging data on a web page. It is a mainly used control in ASP.Net web applications.

 

The Gridview displays data based on the DataSource. There are fews popular DataSource controls available and SqlDataSource control is one of them.

 

The SqlDataSource control can be attached GridView to the SQL Server Database. The SqlDataSource have attributes like ConnectionString, UpdateCommand, DeleteCommand, SelectCommand etc.

 

The following diagram will demonstrate the Data Flow of GridView with SQL DataSource.

 

Asp.net GridView FlowChart

 

The GridView control can perform below features  :

 

  • Binding to data source controls, such as SQL DataSource
  • Sorting Records
  • Format Displayed Data
  • Data Paging
  • Customizable presence through themes and styles.
  • Provide service of row selection capabilities.   
  • Data - Insert, Update and Delete

 

The following namespace is necessary to use the GridView control in the Asp.Net, C#:

 

System.Web.UI.WebControls

 

Syntax

 

The following is the syntax of GridView control in asp.net :

 

<asp:GridView ID="GridView1" runat="server"  > </asp:GridView>



The following are the GridView control main events in the Asp.net,C#:


Event Description
Sorting It used When clicked on the hyperlink to sort a column data.
Sorted It used When clicked on the hyperlink to sort a column data.
SelectedIndexChanged It is used when a row's Select button is clicked.
SelectedIndexChanging It is used when a row's Select button is clicked.
PageIndexChanged It is used when one of the pager buttons is clicked.
PageIndexChanging It is used when one of the pager buttons is clicked.
RowCommand It is used when a button is clicked in the GridView control.
RowDataBound It is used when a data row is bound to data in the GridView control.
RowDeleting It is used when a row's Delete button is clicked.
RowDeleted It is used when a row's Edit button is clicked.
RowUpdated It is used when a row's Update button is clicked.
RowUpdating It is used when a row's Update button is clicked.
RowCancelingEdit   It is used when a row's Cancel button is clicked.
RowCreated It is used when a new row is created in the GridView control.


In this chapter, we will guide the GridView control with the example to display data of DataSource by using DataTable in the GridView.




Example



GridView.aspx

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

</head>

<body>

    <form id="form1" runat="server">

    <p>

       <h2> Asp.net GridView  Example</h2>

 

  <asp:GridView id="GridView1" runat="server">  </asp:GridView>

    </form>

</body>

</html>

 


GridView.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 GridView : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

            DataTable table = new DataTable();

            table = CreateDataTable();

            GridView1.DataSource = table ;

            GridView1.DataBind();

    }

    private DataTable CreateDataTable()

    {

 

        DataTable dt = new DataTable();

 

        DataRow dr = null;

 

        //Create the Columns Definition

 

        dt.Columns.Add(new DataColumn("Sr. No ", typeof(string)));

 

        dt.Columns.Add(new DataColumn("Name", typeof(string)));

 

        dt.Columns.Add(new DataColumn("Country", typeof(string)));

 

        //Add the first Row to each columns defined

 

        dr = dt.NewRow();

 

        dr["Sr. No "] = "1";

 

        dr["Name"] = "Adam Creek";

 

        dr["Country"] = "New York, USA";

 

        dt.Rows.Add(dr);

 

        //Add the second Row to each columns defined

 

        dr = dt.NewRow();

 

        dr["Sr. No "] = "2";

 

        dr["Name"] = "Smith Filter";

 

        dr["Country"] = "London, UK";

 

        dt.Rows.Add(dr);

 

        return dt;

    }

    }

    






Result


Refer below Asp.net "GridView" control example:

 

 

Asp.net GridView Example

 
Sr. No Name Country
1 Adam Creek New York, USA
2 Smith Filter London, UK