Ok, I know this cant really be that hard, but im having trouble finding any info. I have a gridview on the page that i am filling with data based on a user selected date range(a dropdown list). When the user clicks the button i fill the gridview and display it. This is all done using Linq to Sql. I need to impliment paging and sorting as well. Help PLEASE!!! Below is my button click event… i am open to any suggestions to get this working
protected void btnGenerate_Click(object sender, EventArgs e)
{
int dateRange =0;
if (rbDateList.Checked)
{
switch (ddlDateRange.SelectedIndex)
{
case 0:
dateRange = 30;
break;
case 1:
dateRange = 60;
break;
case 2:
dateRange = 90;
break;
default:
dateRange = 30;
break;
}
}
GYTDataContext gt = new GYTDataContext();
var productList = from o in gt.PurchaseOrderDetails
join p in gt.Products on o.ProductId equals p.ProductId
join h in gt.PurchaseOrderHeaders on o.PurchaseOrderId equals h.PurchaseOrderId
where h.OrderDate>DateTime.Now.AddDays(-dateRange)
group o by o.ProductId into orderedItems
select new
{
orderedItems.Key,
QuantityOrdered = orderedItems.Sum(s => s.OrderQuantity)
};
var totalOrderInfo = from p in productList
join prod in gt.Products
on p.Key equals prod.ProductId
select new
{
prod.Reference,
UnitPrice = prod.Price,
prod.ManufacturerProductId,
p.QuantityOrdered,
TotalCost = prod.Price * Convert.ToInt32(p.QuantityOrdered)
};
gvOrderReport.DataSource = totalOrderInfo;
gvOrderReport.DataBind();
gvOrderReport.Visible = true;
Best Answer
I had the same problem as you. Most examples of linqdatasource, ilustare the WhereParameters > controlparameter functionality which is cool but not that powerful.
The answer is simple: Use a LinqDataSource and just implement the "onselecting" event pass whatever kind of data you wish.
Here is a short example with full filtering-paging-ordering capabilities (also note that the populated sql is optimal and only requests top 10 records every time)
ASPX:
CODEBEHIND: