filter on gridview

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 115
Reputation: ninjaimp is an unknown quantity at this point 
Solved Threads: 6
ninjaimp ninjaimp is offline Offline
Junior Poster

filter on gridview

 
0
  #1
May 1st, 2009
While i thought this would be really simple im really struggling on filtering a gridview twice!

I can bind data to my gridview on the choice of my first dropdown box from a dataset

but i would like to then filter those results from a selection in a second dropdown box but for the life of me i cant get it to work!

  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2. If IsPostBack Then
  3.  
  4. Me.drpDecision.Items.Clear()
  5.  
  6. Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("~") & "\App_Data\referrals.mdb;")
  7.  
  8. Dim sql As String = "SELECT * FROM tbl_ WHERE RefService = '" & Me.drpCriteria.SelectedValue & "'"
  9.  
  10. 'Dim SQL As String = "SELECT * FROM tbl_Service WHERE RefService LIKE '" & Me.drpCriteria.SelectedValue & "'"
  11.  
  12. Dim da As New OleDbDataAdapter(sql, conn)
  13. Dim ds As New DataSet
  14.  
  15. da.Fill(ds)
  16.  
  17. GridView1.DataSource = ds
  18. GridView1.DataBind()
  19. MsgBox("Selection")
  20.  
  21. Dim sql2 As String = "select Distinct(RefDecision) FROM tbl_Service WHERE RefService = '" & Me.drpCriteria.SelectedValue & "'"
  22. Dim da2 As New OleDbDataAdapter(sql2, conn)
  23. Dim ds2 As New DataSet
  24. Dim i As Integer
  25.  
  26. da2.Fill(ds2)
  27.  
  28. If ds2.Tables(0).Rows.Count > 0 Then
  29.  
  30. For i = 0 To ds2.Tables(0).Rows.Count - 1
  31. Me.drpDecision.Items.Add(ds2.Tables(0).Rows(i).Item(0))
  32. Next
  33.  
  34.  
  35. Else
  36. Me.drpDecision.Items.Add("No matching records")
  37. End If
  38.  
  39. If Me.drpDecision.SelectedValue <> "" Then
  40. MsgBox("Selection")
  41. FilterSearch(ds)
  42. End If
  43. End If
  44. End Sub
  45.  
  46. Private Sub FilterSearch(ByRef ds As DataSet)
  47.  
  48. GridView1.Dispose()
  49. Dim SearchTB As DataTable = ds.Tables(0)
  50.  
  51. Dim Rows As DataRow() = SearchTB.Select("RefDecision = " & Me.drpDecision.SelectedValue)
  52.  
  53. MsgBox(SearchTB.Rows.Count)
  54.  
  55. GridView1.DataSource = SearchTB.DataSet
  56. GridView1.DataBind()
  57.  
  58. End Sub
  59.  
Please mark this post as 'Solved' if it has helped and fixed your issue.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 11
Reputation: tonym001 is an unknown quantity at this point 
Solved Threads: 0
tonym001 tonym001 is offline Offline
Newbie Poster

Re: filter on gridview

 
0
  #2
May 1st, 2009
Hi ninj

First of all, put your data bind logic in a separate sub not in the page load event you will be needing it again.

Next call the new sub from the page load to make sure it works,

In the designer, double click the second dropdown box - it should take you to the OnSelectionChanged sub in the code behind- enter the name of the sub you created above.

If this does not work correctly it's probably because you are not storing the original value in the session object.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 11
Reputation: tonym001 is an unknown quantity at this point 
Solved Threads: 0
tonym001 tonym001 is offline Offline
Newbie Poster

Re: filter on gridview

 
0
  #3
May 1st, 2009
Sorry, incomplete answer - you obviously need to reset your search criteria by the value of the second dropdown before calling the sub. If you are using Ajax make sure the viewstate is set correctly.
Last edited by tonym001; May 1st, 2009 at 4:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 115
Reputation: ninjaimp is an unknown quantity at this point 
Solved Threads: 6
ninjaimp ninjaimp is offline Offline
Junior Poster

Re: filter on gridview

 
0
  #4
May 2nd, 2009
thanks tony - ill give it a go and let u know how i get on!
Please mark this post as 'Solved' if it has helped and fixed your issue.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC