how to bind generic list to gridview | Bind Database values to Generic list using asp.net | Binding custom generic class list to Gridview
Description:
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 asp.net.
First Create new website and Design your aspx page like this
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Bind Generic List to Gridview</title>
</head>
<body>
<form id="form1"runat="server">
<div>
<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" />
<Columns>
<asp:BoundField HeaderText="UserName" DataField="UserName" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" />
<asp:BoundField HeaderText="LastName" DataField="LastName" />
<asp:BoundField HeaderText="Location" DataField="Location" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|
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)
{
if(!IsPostBack)
{
BindUserDetails();
}
}
protected void BindUserDetails()
{
objUserDetails = GetUserDetails();
gvDetails.DataSource = objUserDetails;
gvDetails.DataBind();
}
protected List<UserDetails> GetUserDetails()
{
string strConnection = "Data Source=MukramJahDB;User Id="sqlSa"; Password="Sql";Initial Catalog=mukramJahDB";
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
if(dt.Rows.Count>0)
{
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();
objUserDetails.Add(userinfo);
}
}
return objUserDetails;
}
|
private int _prop1;
public int Prop1
{
get { return _prop1; }
set { _prop1 = value; }
}
private string _prop2;
public string Prop2
{
get { return _prop2; }
set { _prop2 = value; }
}
protected override object SaveControlState()
{
object[] state = new object[2]; // save the 2 properties
state[0] = _prop1;
state[1] = _prop2;
return state;
}
protected override void LoadControlState(object savedState)
{
object[] state = (object[])savedState;
_prop1 = (int)state[0];
_prop2 = (string)state[1];
}