Solved

Edit search results in Gridview - Wrong row in edit mode

Posted on 2007-03-26
6
476 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now