Solved

Edit search results in Gridview - Wrong row in edit mode

Posted on 2007-03-26
6
491 Views
Last Modified: 2013-11-07
Hi.  I'm using Visual Studio 2005 with VB.

I have a gridview using a SQLDataSource  that allows users to edit and delete records.  That all works fine.  Now I am adding search functionality so that users can search on first and last names and date of birth using input controls that sit above the gridview.  That works fine too and the gridview displays the appropriate records matching the search criteria.  I modify the SelectCommand of the SQLDataSource when the "Find" button is clicked in order to retrieve matching records.

The problems start when I try to edit a record that has been returned by the search functionality.  When I click edit for a record, the gridview refreshes and displays ALL records and the row in edit mode is not the one that I selected.  Obviously the row ID is being used to decide which row should be in edit mode but because the result set has changed it's the wrong row.

I've tried using the Page_Load event to check if the search fields are populated, and if so, modify the SelectCommand of the SQLDataSource appropriately, but this has no effect.

How do I resolve this?  If my search has returned only one record then if I edit that record it should go into edit mode and still be the only record in the gridview.  

All help very much appreciated.
Cheers,
Danik
0
Comment
Question by:Danik_S
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:mzalewski
ID: 18797841
Have you tried setting it to not populate the datagrid on Postback?
if (!Page.IsPostback) {
datagrid.DataBind()
}
0
 

Author Comment

by:Danik_S
ID: 18797864
Hi mzalewski

I haven't tried that, but being a gridview (not a datagrid) it takes care of its own binding automatically. So adding that code wouldn't change anything, would it?

Cheers,

Danik
0
 
LVL 5

Accepted Solution

by:
mzalewski earned 125 total points
ID: 18797915
Sorry, should have re-read your question.

Instead of modifying the SQL select statement, try using the SQLDataSource.FilterExpression Property.

Example:
 <asp:SqlDataSource
                id="SqlDataSource1"
                runat="server"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
                FilterExpression="Title='{0}'">
                <FilterParameters>
                    <asp:ControlParameter Name="Title" ControlId="DropDownList1" PropertyName="SelectedValue"/>
                </FilterParameters>
            </asp:SqlDataSource>

Dont know if it will work, but its probably worth a try.
Here's a link to where I found the example:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.filterexpression.aspx
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:Danik_S
ID: 18813039
Thanks mzalewski - it is starting to come together.

Do you know how I can get the filtering to work with more than one filter?  It works fine with just FirstName, but when I add LastName and/or date of birth, it doesn't seem to work at all.

Cheers,

Danik

0
 
LVL 5

Expert Comment

by:mzalewski
ID: 18813307
You might have to dynamically generate the FilterExpression. If you're setting it like this:
FirstName='{1}' AND LastName='{1}'
But only one of those fields are being filled in:
FirstName='John' AND LastName=''

You could try this:
FilterExpression="(FirstName='{0}' OR '{0}'='') AND (LastName='{1}' OR '{1}'='') "

That would return true if FirstName found a match, or Firstname's value is empty.

0
 

Author Comment

by:Danik_S
ID: 18853383
Thanks mzalewsk - that worked perfectly!

I just had to set the convertEmptyStringToNull property to false for the controls used in the filtering.

Cheers,

Danik
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question