4

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;

2 Answers 2

5

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();
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Ive got two problems with using either of the datasources(linqdatasource or objectdatasource). problem 1 is how do i create a query like im using for use in a datasource, perhaps there is a shorter or more direct method to what im trying to do. problem two is how do i handle the click event

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.