Introduction

The GridView control displays the values of a data source in a table. Each column represents a field, while each row represents a record. The GridView control supports the following features:

  • Binding to data source controls, such as SqlDataSource.
  • Built-in sort capabilities.
  • Built-in update and delete capabilities.
  • Built-in paging capabilities.
  • Built-in row selection capabilities.
  • Programmatic access to the GridView object model to dynamically set properties, handle events, and so on.
  • Multiple key fields.
  • Multiple data fields for the hyperlink columns.
  • Customizable appearance through themes and styles.

Creating a GridView

<asp:GridView ID=”gridView1″ runat=”server”>
</asp:GridView>

This article shows how to use a GridView control in ASP.Net using C# code behind. In this, we perform the following operations on GridView.

  • Bind data to GridView column
  • Edit data in GridView
  • Delete rows from GridView
  • Update row from database

ASPX page code:-

  1. %@ Page Language=“C#” AutoEventWireup=“true”  CodeFile=“Default.aspx.cs” Inherits=“_Default” %>
  2.     <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#8221;>
  3.     <html xmlns=http://www.w3.org/1999/xhtml&#8221;>
  4.     <head id=“Head1” runat=“server”>
  5.         <title>Untitled Page</title>
  6.         <style type=“text/css”>
  7.             .Gridview1{
  8.                 font-family: Verdana;
  9.                 font-size: 10pt;
  10.                 font-weight: normal;
  11.                 color: black;
  12.             }
  13.         </style>
  14.         “text/javascript”>
  15.         
  16.     </head>
  17.     <body>
  18.         <form id=“form1” runat=“server”>
  19.             
  20.                 “GridView1” runat=“server” AutoGenerateColumns=“false” DataKeyNames=“id” OnPageIndexChanging=“GridView1_PageIndexChanging” OnRowCancelingEdit=“GridView1_RowCancelingEdit” OnRowDeleting=“GridView1_RowDeleting” OnRowEditing=“GridView1_RowEditing” OnRowUpdating=“GridView1_RowUpdating”>
  21.                     
  22.                         “id” HeaderText=“S.No.” />
  23.                         “name” HeaderText=“Name” />
  24.                         “address” HeaderText=“address” />
  25.                         “country” HeaderText=“Country” />
  26.                         “true” />
  27.                         “true” />
  28.                      
  29.                 
  30.