Solved

Edit search results in Gridview - Wrong row in edit mode

Posted on 2007-03-26
6
481 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

22 Experts available now in Live!

Get 1:1 Help Now