aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

how to bind generic list to gridview | Bind Database values to Generic list using | Binding custom generic class list to Gridview

Jul 8, 2011

Here I will explain how to bind generic list to gridview and how to bind database values to generic list using


In previous article I explained clearly about what is WCF and how we can use WCF in our applications. Now I will explain how to use Lists in our application to bind data to gridview and I will explain how to bind database values to generic list using 

First Create new website and Design your aspx page like this 

<html xmlns="">
<head runat="server">
<title>Bind Generic List to Gridview</title>
<form id="form1" runat="server">
<asp:GridView runat="server" ID="gvDetails" AutoGenerateColumns="false">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<asp:BoundField HeaderText="UserName" DataField="UserName" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" />
<asp:BoundField HeaderText="LastName" DataField="LastName" />
<asp:BoundField HeaderText="Location" DataField="Location" />
After that add one class file to your website for that Right click on your website and select Add New Item à one window will open in that select Class file and give name as UserDetails.cs because here I am using this name in my sample if you want to give another name change UserDetails reference name in your sample.
Now open your Class file UserDetails.cs and write the following code
After that write the following code in code behind

public class UserDetails
string username = string.Empty;
string firstname = string.Empty;
string lastname = string.Empty;
string location = string.Empty;

public string UserName
get { return username; }
set { username = value; }

public string FirstName
get { return firstname; }
set { firstname = value; }

public string LastName
get { return lastname; }
set { lastname = value; }

public string Location
get { return location; }
set { location = value; }
After completion writing code in your class file open your Default.aspx.cs page add the following namespaces
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
After that write the following code in code behind

List<UserDetails> objUserDetails = new List<UserDetails>();
protected void Page_Load(object sender, EventArgs e)
protected void BindUserDetails()
objUserDetails = GetUserDetails();
gvDetails.DataSource = objUserDetails;
protected List<UserDetails> GetUserDetails()
string strConnection = "Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB";

DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(strConnection);
SqlCommand cmd = new SqlCommand("select * from UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
for(int i=0;i<dt.Rows.Count;i++)
UserDetails userinfo = new UserDetails();
userinfo.UserName = dt.Rows[i]["UserName"].ToString();
userinfo.FirstName = dt.Rows[i]["FirstName"].ToString();
userinfo.LastName = dt.Rows[i]["LastName"].ToString();
userinfo.Location = dt.Rows[i]["Location"].ToString();
return objUserDetails;

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...

nice Article. Every one can understand easily

Anonymous said...

Does this example work with importing data from multiple tables and displaying in one grid?
I have use above example with multiple tables in select query but it's giving error.
A field or property with the name 'XX' was not found on the selected data source."

Anonymous said...

Ok all good it does work with multiple tables.. awesome example....

D.K.N.S.T said...

but this is useless ,can you tell me the live example where we need the genric,because this can we done by direct bindin colmn fiels to grid column

Anonymous said...

Now how to delete a item from generic list and again bind to grid

arjun said...

telugu valla satta chupistunnav..googlelo asp articles anni neeve chustunnaru...keep rocking suresh....all the good luck....

Anonymous said...

Sir Can You Please Help Me Out in Binding the Data from datatable in mysql to ListView in C# Windows Application...

Anonymous said...

Congratulations for your blog! It is really useful.
Can you explain how to fill only first column and first row with data from sql server?
Sorry for my english. Thank you!

Anonymous said...

how to store one by one row dinamically in two dimensional array and display them in using c#.............
Please Me............................

Anonymous said...


subject :
Bind grid when table record updated @ our side or any other user updating it

Read the senario carefully.

I have the grid and bind it using LINQ. Now when i update record in grid that time i rebind and get updated record. but problem in this case when any other user change the record and i no refresh the grid and try to update it that time i seen old record in grid. I want solution when table data is change either my side or any othe user side that time 1 trigger is fire and bind the grid automatic

Is it possible? I know the how to bind grid do not post such a solution.

Nanthini Elangovan said...

Nice Post,Any doubt i m having any doubt in with c# ,means most of the time i referred your blogs only.its easy to understand..

Anonymous said...

Sir, This Abhinav Singh993 ; Please tell me is it possible to perform CRUD Operations using Generic List in using c# or it is only for the Displaying data, if possible please provide a tutorial over it. thank you.

Anonymous said...

how to insert value into the database using stored procedure and List<>........

Please help me..............

Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.