Here I will explain how to bind a generic list to a GridView and how to bind database values to a generic list using ASP.NET. To do that, first we start with a database table.
Creating Table
Now create a table named UserDetail with columns Name, Address and Country. The table looks like below.

After creating the table, insert some values into the table.
We can build a collection of objects; we first need an object. I tend to use a Class1 object for that example. The Class1 object simply holds some information about a user - Name, Address, and Country.
To do that, add one class file to your website; for that, right-click on your website and select Add New Item; a window will open; in that select Class file and give the name Class1.

The following is the object I'm going to use.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Class2
/// </summary>
public class Class1
{
//
// TODO: Add constructor logic here
//
private string name;
private string address;
private string country;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Address
{
get
{
return address;
}
set
{
address = value;
}
}
public string Country
{
get
{
return country;
}
set
{
country = value;
}
}
}
Now create a new website and drag and drop a GridView control onto the aspx page. The GridView code looks like this.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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">
<title></title>
</head>


Comments
Join the conversation! Your thoughts help the community grow.