Here I will explain how to bind generic list to gridview and 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;
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=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB";
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;
}
C# GridView data binding to generic list
ReplyDelete