How to populate GridView from code behind?

Sheonarayan
Posted by in ASP.NET category on for Beginner level | Points: 250 | Views : 57380 red flag

In this article, we shall learn how to populate GridView from code behind. This tutorials is for beginners.

Introduction

GridvIew control is a powerful data grid control that allows us to display the data in tabular format with
sorting and pagination. It also allows us to manipulate the data as well. 

Working with the Code

Get hundreds of ASP.NET Tips and Tricks and ASP.NET Online training here.

In order to populate GridView from the code behind using a data source, we can follow this approach.

Below is my ASPX code that contains a simple GridView.

ASPX PAGE

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

In the above code we have simply kept a GridView control with runat server and id attributes.

CODE BEHIND

string _connStr =

ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

 

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

GetData();

}

}

private void GetData()

{

DataTable table = new DataTable();

 

// get the connection

using (SqlConnection conn = new SqlConnection(_connStr))

{

// write the sql statement to execute

string sql = "SELECT AutoId, FirstName, LastName, Age, Active FROM

PersonalDetail ORDER By AutoId";

// instantiate the command object to fire

using (SqlCommand cmd = new SqlCommand(sql, conn))

{

// get the adapter object and attach the command object to it

using (SqlDataAdapter ad = new SqlDataAdapter(cmd))

{

// fire Fill method to fetch the data and fill into DataTable

ad.Fill(table);

}

}

}

// specify the data source for the GridView

GridView1.DataSource = table;

// bind the data now

GridView1.DataBind();

}

In the code behind, we have used ADO.NET objects to retrieve the data from the database to a DataTable and that DataTable (table variable) is being used as the DataSource for the GridView. Just setting the DataSource is not enough; we need to call the DataBind method of the GridView that actually binds the data to the GridView.

OUTPUT

When we run the page, the output looks something similar to the above picture (naturally if your data will be exactly same as mine ;)).

In the next couple of articles, we shall learn customization of GridView and many other frequently faced problem solutions related with the GridView.

Keep reading and sharing your knowledge. 

Page copy protected against web site content infringement by Copyscape

About the Author

Sheonarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Posted by: Vinay13mar on: 11/4/2012 | Points: 25
Hi this is really nice article. Please check this link which i have found.

http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=2&title= Binding GridView Using C# in Asp.Net

Login to post response

Comment using Facebook(Author doesn't get notification)