943,529 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 6363
  • ASP.NET RSS
May 1st, 2009
0

filter on gridview

Expand Post »
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!

ASP.NET Syntax (Toggle Plain Text)
  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.  
Similar Threads
Reputation Points: 22
Solved Threads: 9
Junior Poster
ninjaimp is offline Offline
129 posts
since Apr 2008
May 1st, 2009
0

Re: filter on gridview

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tonym001 is offline Offline
11 posts
since May 2009
May 1st, 2009
0

Re: filter on gridview

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tonym001 is offline Offline
11 posts
since May 2009
May 2nd, 2009
0

Re: filter on gridview

thanks tony - ill give it a go and let u know how i get on!
Reputation Points: 22
Solved Threads: 9
Junior Poster
ninjaimp is offline Offline
129 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: remote access
Next Thread in ASP.NET Forum Timeline: Help!!!! How can my page will not be disarrange in other display resolution....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC