Solved

Edit search results in Gridview - Wrong row in edit mode

Posted on 2007-03-26
6
521 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

615 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