How to Bind Records Web Application Gridview for Different Approach


In this Article, I am going to explain about how to bind the records using web application in Gridview for Different Approach .I have implemented gridview control for ASP.net web application .I have post source given below

1.How to Bind Records in Gridview Using Datatable Code here




<html>
<head>
Client Side
<asp:GridView runat="server" ID="GrdStatic">
<Columns>
<asp:BoundField DataField="" HeaderText ="Id" />
<asp:BoundField DataField="" HeaderText ="Name" />
</Columns>
</asp:GridView>
</head>
</html>


Server Side
SqlConnection sql = new SqlConnection("Data Source=AspDotnet;Initial Catalog=Test;Integrated Security=True;");
DataTable dt=new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindStatic();
ListData();
BindSql();
IListData();
PopulateData();
}
}
private void BindStatic()
{
DataTable dt=new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("Empno");
DataRow dr;
dr = dt.NewRow();
dr[0] = 1;
dr[1] = "Pawan";
dr[2] = 1500;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 2;
dr[1] = "Asp.net";
dr[2] = 1500;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 3;
dr[1] = "DotnetSpider";
dr[2] = 1500;
dt.Rows.Add(dr);

GrdStatic.DataSource = dt;
GrdStatic.DataBind();
}

2.How to Bind Records in Gridview Using List Object Code here


Client Side
<asp:GridView runat="server" ID="lstGrid"></asp:GridView>
Server Side
private void ListData()
{
List<Emp> _lstEmp=new List<Emp>();
_lstEmp.Add(new Emp() { ID = 1, BookCode = "150", BookName = "Dotnet" });
_lstEmp.Add(new Emp() { ID = 2, BookCode = "151", BookName = "Asp.net" });
_lstEmp.Add(new Emp() { ID = 3, BookCode = "152", BookName = "C#" });
_lstEmp.Add(new Emp() { ID = 4, BookCode = "152", BookName = "Vb.net" });
_lstEmp.Add(new Emp() { ID = 5, BookCode = "152", BookName = "SqlServer" });
lstGrid.DataSource = _lstEmp;
lstGrid.DataBind();

}

3.How to Bind Records in Gridview Using SqlDataSource Code here


Client Side Only
<asp:GridView runat="server" ID="GridView2" DataSourceID="SqlDataSource1"></asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>" SelectCommand="SELECT * FROM [Employees]"></asp:SqlDataSource>

4.How to Bind Records in Gridview Using Data Base Collection Data Code here


Client Side
<br/>

Bind From Data Base Collection Data


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

Server Side
private void BindSql()
{
sql.Open();
SqlDataAdapter sqqladp = new SqlDataAdapter("Select * from Employees", sql);
sqqladp.Fill(dt);
GrdSql.DataSource = dt;
GrdSql.DataBind();
}


5.How to Bind Records in Gridview Using ILIST object here


Client Side
<br/>

Bind From ILIST


<asp:GridView runat="server" ID="GrdIlist"></asp:GridView>
Server Side
private void IListData()
{
IList<Emp> _lstEmp = new List<Emp>();
_lstEmp.Add(new Emp() { ID = 1, BookCode = "150", BookName = "Dotnet" });
_lstEmp.Add(new Emp() { ID = 2, BookCode = "151", BookName = "Asp.net" });
_lstEmp.Add(new Emp() { ID = 3, BookCode = "152", BookName = "C#" });
_lstEmp.Add(new Emp() { ID = 4, BookCode = "152", BookName = "Vb.net" });
_lstEmp.Add(new Emp() { ID = 5, BookCode = "152", BookName = "SqlServer" });
GrdIlist.DataSource = _lstEmp;
GrdIlist.DataBind();

}

5.How to Bind Records in Gridview Using LIST object here


Client Side


Bind From Database Using List


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

Server Side
private List<Emp> PopulateData()
{
using (SqlConnection con = new SqlConnection("Data Source=AspDotnet;Initial Catalog=Test;Integrated Security=True;"))
{
using (SqlCommand cmd = new SqlCommand("select * from TABLEE1", con))
{
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
List<Emp> customers = new List<Emp>();
while (sdr.Read())
{
customers.Add(new Emp());
customers[0].ID = Convert.ToInt32(sdr["ID"]);
customers[0].BookCode = sdr["BookCode"].ToString();
customers[0].BookName = sdr["BookName"].ToString();
}
con.Close();
lstDb.DataSource = customers;
lstDb.DataBind();
return customers;
}
}
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: