GridView with LINQ Programatically
Posted by Tihomir Ivanov on 20 October 2009 17:22
Rating: 0.00
Instead of using LinqDataSource to laod data for GridView you can load data for it programatically:
in the aspx file we have simple GridView:
<asp:GridView ID="_someGV" runat="server" AutoGenerateColumns="false"
<Columns>
<asp:BoundField DataField="..." />
</Columns>
</asp:GridView>
in the code-behind class we add all necessary functionality to load the data using LINQ:
...
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
DBDataContext dbContext;
dbContext = new DBDataContext();
var dataItems = from s in dbContext.someTable
select s;
_someGV.DataSource = dataItems;
_someGV.DataBind();
}
}
catch (Exception)
{}
}
Now, we'll add the paging functionality:
<asp:GridView ID="_someGV" runat="server" AutoGenerateColumns="false"
AllowPaging="true" PageSize="100"
onpageindexchanging="_someGV_PageIndexChanging"
<Columns>
<asp:BoundField DataField="..." />
</Columns>
</asp:GridView>
in the code-behind class handle the PageIndexChaging Event:
protected void _somesGV_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
DBDataContext dbContext;
//t we need to reload all data gaian
dbContext = new DBDataContext();
var dataItems = from s in dbContext.someTable
select s;
_someGV.DataSource = dataItems;
_someGV.PageIndex = e.NewPageIndex;
_someGV.DataBind();
}
catch (Exception)
{}
}
Comments:
No comments yet.