Home All Groups Group Topic Archive Search About

Paging and Filtering in Gridview (.NET 2.0)

Author
11 Jan 2006 3:31 PM
Bill Manring
I am writing a very simple web application using VS 2005 and a SQL Server
Database. I am just starting to experiment wth .NET 2.0 and I am trying out
the supposedly improved databinding.

I dragged a table from a datasource on to my web form and the GridView
cpntrol was created. I turned on paging, and Edit and Delete buttons. I ran
the app and everything worked great - not a single line of code written yet.

Then I wanted to add a simple feature where the user could filter out data
based on a value in a single column. I added a little code that set the
FilterExpression property of the SqlDataSource, and that seemed to work fine
as well. Here is the FilterExpression code:

SqldataSource1.FilterExpression = "LastName = '" + txtFilterValue.Text + "'";

This worked fine - I got a subset of the original data based on the last
name I entered into a text box.

The problem began if I tried to edit a record in the filtered data or go to
another page in the filtered data. Taking either of these actions resulted
in the restoration of the original unfiltered dataset. If I hit Edit on the
first record of the first page in the filtered dataset, I was switched back
to the first record in the unfiltered dataset and put in edit mode in the
GridView control. If I go to another page of data in the Filtered dataset,
the original dataset is resotred and I am sent to that page number in the
original dataset.

I tried taking the approach of changing the SelectCommand property of the
SqlDataSource object instead of using the FilterExpression property, but that
behaved the same way.

The only code I have is in the "Filter" button click event. I have no code
at all in the page_load event.

This seems like it should be simple, but I am obviously missing something.
I'm pretty sure I could go back to my current .NET v1.1 habits of doing
everything myself in code, but I want to give the new databinding a chance
before I abandoned it.

Anyone have any idea what I am doing wrong?

--
Thanks,

Bill Manring

Author
11 Jan 2006 6:29 PM
Wouter van Vugt
Hi Bill,

I am not sure because I haven't tried your scenario yet. I believe it
might be built in behavior of the GridView. But like I said, I am not
entirely sure on this.

Grtz,

Wouter van Vugt
Trainer - Info Support
http://blogs.infosupport.com/wouterv
Are all your drivers up to date? click for free checkup

Author
11 Jan 2006 6:35 PM
Phillip Williams
You might just need to move the line of code that sets the filter condition
to the page_load so that the filter would be set upon each PostBack.
Show quoteHide quote
"Bill Manring" wrote:

> I am writing a very simple web application using VS 2005 and a SQL Server
> Database. I am just starting to experiment wth .NET 2.0 and I am trying out
> the supposedly improved databinding.
>
> I dragged a table from a datasource on to my web form and the GridView
> cpntrol was created. I turned on paging, and Edit and Delete buttons. I ran
> the app and everything worked great - not a single line of code written yet.
>
> Then I wanted to add a simple feature where the user could filter out data
> based on a value in a single column. I added a little code that set the
> FilterExpression property of the SqlDataSource, and that seemed to work fine
> as well. Here is the FilterExpression code:
>
> SqldataSource1.FilterExpression = "LastName = '" + txtFilterValue.Text + "'";
>
> This worked fine - I got a subset of the original data based on the last
> name I entered into a text box.
>
> The problem began if I tried to edit a record in the filtered data or go to
> another page in the filtered data. Taking either of these actions resulted
> in the restoration of the original unfiltered dataset. If I hit Edit on the
> first record of the first page in the filtered dataset, I was switched back
> to the first record in the unfiltered dataset and put in edit mode in the
> GridView control. If I go to another page of data in the Filtered dataset,
> the original dataset is resotred and I am sent to that page number in the
> original dataset.
>
> I tried taking the approach of changing the SelectCommand property of the
> SqlDataSource object instead of using the FilterExpression property, but that
> behaved the same way.
>
> The only code I have is in the "Filter" button click event. I have no code
> at all in the page_load event.
>
> This seems like it should be simple, but I am obviously missing something.
> I'm pretty sure I could go back to my current .NET v1.1 habits of doing
> everything myself in code, but I want to give the new databinding a chance
> before I abandoned it.
>
> Anyone have any idea what I am doing wrong?
>
> --
> Thanks,
>
> Bill Manring
>
Author
13 Jan 2006 12:22 AM
Bill Manring
Phillip,

That worked - thanks.
--
Thanks,

Bill Manring


Show quoteHide quote
"Phillip Williams" wrote:

> You might just need to move the line of code that sets the filter condition
> to the page_load so that the filter would be set upon each PostBack.
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Bill Manring" wrote:
>
> > I am writing a very simple web application using VS 2005 and a SQL Server
> > Database. I am just starting to experiment wth .NET 2.0 and I am trying out
> > the supposedly improved databinding.
> >
> > I dragged a table from a datasource on to my web form and the GridView
> > cpntrol was created. I turned on paging, and Edit and Delete buttons. I ran
> > the app and everything worked great - not a single line of code written yet.
> >
> > Then I wanted to add a simple feature where the user could filter out data
> > based on a value in a single column. I added a little code that set the
> > FilterExpression property of the SqlDataSource, and that seemed to work fine
> > as well. Here is the FilterExpression code:
> >
> > SqldataSource1.FilterExpression = "LastName = '" + txtFilterValue.Text + "'";
> >
> > This worked fine - I got a subset of the original data based on the last
> > name I entered into a text box.
> >
> > The problem began if I tried to edit a record in the filtered data or go to
> > another page in the filtered data. Taking either of these actions resulted
> > in the restoration of the original unfiltered dataset. If I hit Edit on the
> > first record of the first page in the filtered dataset, I was switched back
> > to the first record in the unfiltered dataset and put in edit mode in the
> > GridView control. If I go to another page of data in the Filtered dataset,
> > the original dataset is resotred and I am sent to that page number in the
> > original dataset.
> >
> > I tried taking the approach of changing the SelectCommand property of the
> > SqlDataSource object instead of using the FilterExpression property, but that
> > behaved the same way.
> >
> > The only code I have is in the "Filter" button click event. I have no code
> > at all in the page_load event.
> >
> > This seems like it should be simple, but I am obviously missing something.
> > I'm pretty sure I could go back to my current .NET v1.1 habits of doing
> > everything myself in code, but I want to give the new databinding a chance
> > before I abandoned it.
> >
> > Anyone have any idea what I am doing wrong?
> >
> > --
> > Thanks,
> >
> > Bill Manring
> >

Bookmark and Share