• home
  • Email: trainings@synapseindia.careers



Working With ASP.NET GridView Control

Mar 15, 2016

GridView control is providing an easy approach for displaying all data from your database into tabular format. GridView works fine with SqlDataSource data control for making connection and executing SQL query into server. In this example we have displayed all data from database into GridView by binding it to SqlDataSource, See the code below:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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">
<form id="form1" runat="server">

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:myconnection %>"
SelectCommand="select * from employee1">


<br />
<asp:GridView ID="GridView2" runat="server" AllowSorting="True"
AutoGenerateColumns="False" AutoGenerateSelectButton="True" DataKeyNames="id"
DataSourceID="SqlDataSource1" EnablePersistedSelection="True"
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="ename" HeaderText="ename" SortExpression="ename" />
<asp:BoundField DataField="department" HeaderText="department"
SortExpression="department" />
<asp:BoundField DataField="designation" HeaderText="designation"
SortExpression="designation" />
<asp:BoundField DataField="doj" HeaderText="doj" SortExpression="doj" />
<SelectedRowStyle BackColor="#FF3300" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />


In the above code , we have created our user defined columns and bound it with columns in table at database. You can also see the event "SelectedIndexChanged" added in GridView for tracking the selection of row and on it's basis displaying all records of selected row. See the code for .cs file where we are fetching all the data of selected row.

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

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)

protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)

GridViewRow rw = GridView2.SelectedRow;
Label1.Text = "<h1> Employee Details Selected </h1> <br/>";
Label1.Text += "Employee ID : <b>" + rw.Cells[1].Text + "</b><br/>";
Label1.Text += "Employe Name : <b> " + rw.Cells[2].Text + "</b><br/>";
Label1.Text += "Employe Department : <b>" + rw.Cells[3].Text + "</b><br/>";
Label1.Text += "Employe Designation : <b>" + rw.Cells[4].Text + "</b><br/>";
Label1.Text += "Employe Data of Joining : <b>" + rw.Cells[5].Text + "</b><br/>";


This is very good example for understanding the working of GridView. We saw how GridView can be bound with SqlDataSource and how selected row's value can be displayed on it's selection. In the next article we will explore more about GridView. SynapseIndia Training is providing a good platform for learning .NET. Trainee learns ASP.NET as an important technology from one of the various SynapseIndia Technologies used by SynapseIndia for web development.  

SynapseIndia (CEO: Shamit Khemka)