473,383 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Filtering and Editing in GridView (.NET 2.0)

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

Jan 8 '06 #1
2 8079
=?Utf-8?B?QmlsbCBNYW5yaW5n?= <Bi*********@discussions.microsoft.com>
wrote in news:09**********************************@microsof t.com:
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 am having exact same problem - if you get an answer I would love to hear
it! andrius at warpmail dot net

Thanks in advance
Feb 5 '06 #2
Not sure if you fixed this yet, but I'll give you the answer.

If your trying to use the new ASP.NET 2.0 approach to a "codeless" Gridview you need to do just that.. make it "codeless". You adding the code to the Search button postback event I assume:

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

This is wrong. You have bypassed the "codeless" functionality of the control by doing this. It loses its state when the page is refreshed or repaged. The values you set are not saved in viewstate. So everytime your page posts back you lose your filter expression. You need to put the expression in the control. For example. Say i have a text box with a search button:

<tr><td>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Search" />
</td></tr>

You don't need any code behind the search button. By clicking it you cause a postback to occur which rebinds the state of the control. in the gridview's datasource you need to add:

<asp:SqlDataSource
ID="SqlDataSource3"
runat="server"
ConnectionString="<%$ ConnString %>"
SelectCommand="GetData"
SelectCommandType="StoredProcedure"
FilterExpression="Description like '%{0}%'">
<FilterParameters>
<asp:ControlParameter ControlID="txtSearch" Name="Description" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>

the "filter expression" and "filter parameters" are processed on each postback. As long as there is data in the textbox.. which is saved in viewstate, the grid will be filtered by the expression.

Hope this helps.
Gavin Stevens
MCSD.NET, MCAD.NET, MCSD
Gavin.Stevens@GMail.com
Apr 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
0
by: Mike P | last post by:
I have a couple of columns in my gridview that I am adding formatting to in the RowDataBound event. But when I want to edit the rows when the RowDataBound event gets called, I get an error. I'm...
0
by: Mike P | last post by:
I am trying to edit a gridview while using paging, but whenever I try to edit a row on a page other than page 1, I get an error. Here is my gridview and my code : <asp:GridView ID="GridView1"...
2
by: kevinpond | last post by:
I have a Gridview displaying the results of a stored procedure. The stored procedure is constantly changing so the columns displayed in the data grid are constantly changing. I'd like to give...
3
by: Harry Haller | last post by:
Hello, I want to implement a generic list which will be used to display 7 columns in a GridView. One should be able to sort, filter and page each of the 7 columns. Ideally the filter should be...
1
by: needhelp1 | last post by:
I have gridview with a detailsview below. When I click on 'New' in brings up the DetailsView for inserting. When I click on 'Edit' in does not brink up the DetailsView. What am I doing wrong? I...
0
by: SAL | last post by:
Hello, I have a small Web app that has a couple of pages in it. Both pages have gridviews on them with basically the same ObjectDataSource. This object data source is a business class. One of the...
0
by: lidadil | last post by:
Hello Everyone, i'm having a gridview wich list clients details. i have setup a search text box and a dropdownlist for filtering. Everything is working except when i try to navigate the next...
2
by: SK2010 | last post by:
I have a gridview with edit and delete. When i click Edit, I donot get all the textboxes. I just get 1. on top of that, when I try to update anything in that textbox, I get object Reference not set...
0
by: DeanC | last post by:
I am having a problem when I am updating my rows. The data is being typed in is getting overriden by the data that was previously there. Does anyone know how to fix this? protected void...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.