Reputation: 854
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;
Upvotes: 4
Views: 8680
Reputation: 3356
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:
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<asp:Button ID="btnFilter" runat="server" Text="Filter"
onclick="btnFilter_Click"/>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
onselecting="LinqDataSource1_Selecting">
</asp:LinqDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataSourceID="LinqDataSource1">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" ReadOnly="True"
SortExpression="FirstName" />
<asp:BoundField DataField="MiddleName" HeaderText="MiddleName" ReadOnly="True"
SortExpression="MiddleName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" ReadOnly="True"
SortExpression="LastName" />
</Columns>
</asp:GridView>
CODEBEHIND:
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
var ctx = new LinqDataSource.DBDataContext();
IQueryable<Customer> customers = ctx.Customers;
if (!String.IsNullOrEmpty(txtLastName.Text))
customers = customers.Where ( c => c.LastName.Contains(txtLastName.Text));
e.Result = customers;
}
protected void btnFilter_Click(object sender, EventArgs e)
{
GridView1.DataBind();
}
Upvotes: 5
Reputation: 4399
Since you are using LINQ-to-SQL with a GYTDataContext
, why not use a LinqDataSource
to populate your Gridview?
The LinqDataSource
can handle paging and sorting automatically.
http://msdn.microsoft.com/en-us/library/bb547113.aspx
Upvotes: 1