SiteMap  | About    

Create Gridview at runtime

In Asp.Net one of the most frequently used Web Control is the GridView control. In the previous examples we access data from database using SqlDataSource. So no need of Code behind use to retrieve the data. Also we fixes database , sql and everything before we run the program.

Gridview from SqlDataSource

gridview at runtime

In this article we'll look at how to use the GridView to display data. In order to display data on runtime, simply drag and drop the DataGrid onto your ASP.NET page and rest of the things we set from the code behind. After drag and drop the gridview, your design page look like the following image :

gridview design

After drag and drop the gridview on the form, we go to the code behind section and write the code for accessing data from database and display on Gridview.


In this article we are using Microsoft's Pubs database for retrieving data. You can download Pubs database from the following link:


In this program we are displaying the data from Stores table in the Pubs database. So first create a connection string and then create the sql statement for retrieving the data.


string connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=****";
string sql = "select * from stores";

After retrieving the data in Dataset, you should set the data source for gridview as Dataset.


GridView1.DataSource = ds.Tables[0];

From the following source code, you can create a gridview and fill the data at runtime.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head id="Head1" runat="server">
	<title>Untitled Page</title>
	<form id="form1" runat="server">
		<asp:GridView ID="GridView1" runat="server">

C# Source Code


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
	protected void Page_Load(object sender, EventArgs e)
		SqlDataAdapter adapter = new SqlDataAdapter();
		DataSet ds = new DataSet();
		int i = 0;
		string sql = null;
		string connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=*****";
		sql = "select * from stores";
		SqlConnection connection = new SqlConnection(connetionString);
		SqlCommand command = new SqlCommand(sql, connection);
		adapter.SelectCommand = command;
		GridView1.DataSource = ds.Tables[0];

VB.Net Source Code


Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
	Inherits System.Web.UI.Page
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Dim adapter As New SqlDataAdapter()
		Dim ds As New DataSet()
		Dim i As Integer = 0
		Dim sql As String = Nothing
		Dim connetionString As String = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=*****"
		sql = "select * from stores"
		Dim connection As New SqlConnection(connetionString)
		Dim command As New SqlCommand(sql, connection)
		adapter.SelectCommand = command
		GridView1.DataSource = ds.Tables(0)
	 End Sub
End Class (C) 2018    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.