 |
BOOK: Beginning ASP.NET 4.5 : in C# and VB
 | This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 4.5 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

September 25th, 2013, 08:34 PM
|
|
Authorized User
|
|
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
|
|
LINQ to Entities And Handling Gridview Events
Hi,
I think I searched half of the internet in one month and still can't find the solution...
I'm using LINQ to Entities for searching and then assign it to the DataSource of the Gridview...
The problem is handling sorting and paging events...
I think some people know how to handle these events when using LINQ to SQL, But I need them for LINQ to Entities...
Is it that hard when using LINQ to Entities???
Please tell me if I have to bring more information,
Thanks :)
.....
Edited : I think I had to posted this in general discussion forum, but now I don't know how to move it there... Sorry Mr.Spaanjaars :(...
Last edited by Kourosh94; September 25th, 2013 at 08:49 PM..
|
|

September 28th, 2013, 09:46 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
One way to do this is to use the Dynamic LINQ library that lets you perform string based searches. You can install it using NuGet: https://www.nuget.org/packages/DynamicQuery/ More info can be found here: http://weblogs.asp.net/scottgu/archi...y-library.aspx
The various events then expose data about sorting and paging which you can use to build up your LINQ query.
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

September 28th, 2013, 11:47 AM
|
|
Authorized User
|
|
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
|
|
Hi Imar,
I used the dynamic LINQ when you told me...
But I Couldn't find the events implementation again...
......
Here is an artical I found it similar : http://forums.asp.net/post/1177923.aspx
So until here, I have used LINQ to SQL, LINQ to Entities and Dynamic LINQ for it, but none of them works...
Maybe I'm doing something wrong...
Anyway thank you for answering Imar... I really appreciate it... :)
|
|

September 28th, 2013, 11:55 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
>> But I Couldn't find the events implementation again.
Not sure what you mean with this, nor what the actual problem you're having is.
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

September 28th, 2013, 12:31 PM
|
|
Authorized User
|
|
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
|
|
I mean I used Dynamic LINQ, but still can't figure out how to implement the sorting and paging events...
|
|

September 28th, 2013, 02:03 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Can you post the code you have so far, along with a description of your EF model?
Imar
|
|

September 28th, 2013, 03:04 PM
|
|
Authorized User
|
|
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
|
|
Code for Search Button I've used with LINQ :
protected void bSearch_Click(object sender, EventArgs e)
{
using (CostPriceEntities myEntities = new CostPriceEntities())
{
GridView1.DataSourceID = null;
var data = (from a in myEntities.Assembles
where a.CodeA.Contains(tbCode.Text)
&& a.MESCA.Contains(tbMESC.Text)
&& a.KindA.Contains(ddlKind.Text)
&& a.NameA.Contains(tbName.Text)
select a).ToList();
GridView1.DataSource = data;
GridView1.DataBind();
}
}
Code for Search Button I'm using with Dynamic LINQ
protected void Button1_Click(object sender, EventArgs e)
{
using (CostPriceEntities myEntities=new CostPriceEntities())
{
GridView1.DataSourceID = null;
var data = myEntities.Assembles
.Where("NameA.Contains(@0) AND CodeA.Contains(@1) AND MESCA.Contains(@2) AND KindA.Contains(@3)", tbName.Text,tbCode.Text,tbMESC.Text,ddlKind.Text). ToList();
GridView1.DataSource = data;
GridView1.DataBind();
}
}
My EF has 3 tables : Assemble, Part, Process... they have relationships
And the codes above are using for searching through Assemble table...
with both LINQ or Dynamic LINQ, Gridview need to handle paging and sorting events...
if I have to bring more information, Please tell me...
thanks again:)
|
|

September 28th, 2013, 03:53 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
I think the next steps are as follows:
1. Move the code that loads your data to a separate method.
2. Call that new method from the current events such as Click
3. Handle the Sorting event as explained in the article you linked to
4. From that method, retrieve e.SortExpression and store it in a field or property
5. Call the data loading method from step 1
6. Inside the data loading method, make use of the sort expression by expanding the query with an OrderBy statement that you pass the sort expression.
Hope this helps,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

September 28th, 2013, 04:59 PM
|
|
Authorized User
|
|
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
|
|
Hi there,
I wanna thank you Imar for helping me...
I do everything you told me, and it worked... I just changed a few...
here is the code for sorting event :
using (CostPriceEntities myEntities = new CostPriceEntities())
{
var data = myEntities.Assembles
.Where("NameA.Contains(@0) AND CodeA.Contains(@1) AND MESCA.Contains(@2) AND KindA.Contains(@3)", tbName.Text, tbCode.Text, tbMESC.Text, ddlKind.Text)
.OrderBy(e.SortExpression).ToList();
GridView1.DataSource = data;
GridView1.DataBind();
}
the sortdirection always is Ascending, after some searched in internet, I found it out the gridview only change the sortdirection when it's datasource assign to a datasource control...
But anyway, Now my Nightmare(sorting gridview when using LINQ) is gone thanks to you Imar...
Hope this post helps someone in future...
|
|

September 29th, 2013, 06:23 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You can find a solution to support ascending and descending order with the GridView here: http://stackoverflow.com/questions/2...ways-ascending
Cheers,
Imar
|
|
 |
|