Data Bind To GridView and apply Pagging using DATATABLE to LINQ
02:31
Here I am Explaining Databind to GridView and apply Pagging for GridView using DataTale to LINQ.
1.Create new website and add page to your website in VISUAL STUDIO.
2.Design the page like this
1.Create new website and add page to your website in VISUAL STUDIO.
2.Design the page like this
<%@
Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grid" runat="server" AllowPaging="true" PageSize="10" OnPageIndexChanging="grid_PageIndexChanging">
</asp:GridView>
</div>
</form>
</body>
</html>
3.Add the below code to your .cs file of the webpage
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if (!IsPostBack)
{
GetProjectPool();
}
}
public
void GetProjectPool()
{
SqlConnection con = new SqlConnection("server=ADMINISTRATOR00;database=ProjectsHub;User
Id=eoi_prasanna;password=987654");
SqlDataAdapter adapter = new SqlDataAdapter("select
*from ProjectsPool", con);
DataTable table = new DataTable();
DataTable table1 = new DataTable();
adapter.Fill(table);
var query = from row in table.AsEnumerable() select
row;
table1 =
query.CopyToDataTable();
DataView view = table1.DefaultView;
grid.DataSource = view;
grid.DataBind();
}
protected
void grid_PageIndexChanging(object sender, GridViewPageEventArgs
e)
{
grid.PageIndex =
e.NewPageIndex;
GetProjectPool();
}
}
4.Then run your application or press F5.
the screen appears like below
then press on 2nd page then the 2nd page shows like below
enjoy Happy coding...takecare.
C# binding GridView control to Datatable in .NET application
ReplyDelete