Solved

Edit search results in Gridview - Wrong row in edit mode

Posted on 2007-03-26
6
494 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Title # Comments Views Activity
Technology Resume 7 71
Adding where clause to entity statement c# 1 49
vb.net datagrid point 4 23
Send parameters via datatable ajax call 4 12
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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