Link to home
Start Free TrialLog in
Avatar of FMabey
FMabeyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Dataview Filter Alternative

Hi all,

I am currently using dataview rowfilters to change the data shown in a datagridview.

I basically have two datagridviews on a form. One (DGV_RFI), has a list of contract numbers 200 to 500, with each contract number having idents ranging from 1 to 100. When the user scrolls through this list of idents I would like my other Datagridview (DGV_RFIDraw) to change to show records relating to the contract and ident currently selected by the user in DGV_RFI.

DGV_RFI is bound to a SQL Server table and DGV_RFIDraw is bound to a SQL Server View. Both DGV's use a dataview to display their data (The dataviews are bound to datasets).

Currently I am using the following code to filter DGV_RFIDraw:


Private Sub DGV_RFI_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_RFI.CellEnter

DV_RFIDraw.RowFilter = "Cont = " & TXT_RFIContract.Text & " AND RFI = " & TXT_RFI.Text & ""
     
End Sub

This code works but as the user scrolls through the list (DGV_RFI) it is very slow.

Can anyone think of an alternative way of making this smoother?

Cheers
ASKER CERTIFIED SOLUTION
Avatar of VBRocks
VBRocks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of David L. Hansen
You can really streamline things by having your second grid simply receive the data from the selected cell of the first grid, instead of going through a database View.  When a row becomes selected in the first grid, you could push the selected data to the other grid through the DGV_RFI_SelectionChanged event.
"selected row or rows" that is...sorry.
Avatar of FMabey

ASKER

VBRocks,

Do you mean by using something like this?

            DS_RFIDraw1.Clear()
            SQL_RFIDraw.Parameters("@Cont").Value = "" & TXT_RFIContract.Text & ""
            SQL_RFIDraw.Parameters("@RFI").Value = "" & TXT_RFI.Text & ""
            SDA_RFIDraw.SelectCommand = SQL_RFIDraw
            SQL_RFIDraw.ExecuteNonQuery()
            SDA_RFIDraw.Fill(DS_RFIDraw1)
Avatar of FMabey

ASKER

Sorry... SQL_RFIDraw is a SQLCommand which reads:

SELECT     RFI, Cont, Str, Drawing#, DC
FROM         VW_RFI_DRAWINGS2
WHERE     (Cont = @Cont) AND (RFI = @RFI)
I think creating a database connection to sql, running query, and populating results for each cellenter event would be EVEN slower than filtering the grid. Try moving the code from CellEnter event to SelectionChanged event. This way, it would fire only on ROW selection changes and not on CELL selection changes (you may click on a different cell within the same row and it would do all the filtering again!!!).

Also, i am not sure how you get the values for the filter here
DV_RFIDraw.RowFilter = "Cont = " & TXT_RFIContract.Text & " AND RFI = " & TXT_RFI.Text & ""

i would use the selectedrows of the grid like this

DV_RFIDraw.RowFilter = "Cont = " & dgv.SelectedRows(0).Cells("columnname").Value & " AND RFI = " & dgv.SelectedRows(0).Cells("columnname").Value & ""
I guess you should also consider whether new idents may be added for any given contract AFTER the grids are loaded.  If so, you'll want to requery every time the row selection changes in order to show the latest information.  So, If you are presenting enough data for performance to be impacted by a lot of hits to the database or you know that the idents won't be changing very often, then filtering would be fine.
FMabey - my apologies, I didn't catch your question when you posted it.  Do you still need some help with that, or have you got it figured out now?

Avatar of FMabey

ASKER

VBRocks,

No worries. Yep, I've sorted it now. I did have a play around with dataview filters a bit more with some of the suggestions from the other guys but your suggestion, along with tidying up some of the views I was using has sorted my issue.

Thanks again
Cool.  

You know, loading all of your data into a DataView, and just filtering the view works great, when you don't have very many records.  But, when you have a lot of records to work with, it's much faster to query the database just for the records that you need.  

I've proved this over and over again.  In fact, I'm working on a very large database now that implements that approach.

Just for your info, you can also use an SqlDataReader, which retrieves the data faster than loading a DataSet using an adapter, but it's read-only, and once you receive it, you still have to loop through each row and do something with it, like load it into a grid or table.