2014年7月13日 星期日

[RESOLVED] Filter gridview data for a given date range


Hi everyone I'm working on a project where my submission date is drawing near. I'm using vb.net language which in this case I'm working with for the very first time.


I appreciate anyone who could help me solve these errors I'm facing with regards to filtering data in the gridview.


Currently I have populated two drop down lists, two calendar controls and one search button to help filter the data in my gridview, however I'm not able to filter the gridview data based on the date selected from my two calendar controls


(*** to be more precise, I need to get all the records from my access database for a given date range which user selects from the calendars, for e.g. 20 May 2013 - 4 July 2013 but I'm only working with one date/time column in the access database)


I have searched everywhere for an example on how to go about doing this but could not find any working solution.


**I am using microsoft access database with a date/time column


Here is what I have done up :


Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click


'Date Search



If MultiView1.ActiveViewIndex = 0 Then


Dim FromDate As String

Dim ToDate As String




FromDate = Calendar1.SelectedDate

ToDate = Calendar2.SelectedDate




If FromDate <> "" And ToDate = " Then" Then


Session("Purchase Search String") = " And PurchaseDate between #" & FromDate & "# and #" & Date.Now.ToString("dd/MM/yyyy") & "#"


ElseIf FromDate = "" And ToDate = "" Then


Session("Purchase Search String") = ""


ElseIf FromDate <> "" And ToDate <> "" Then


Session("Purchase Search String") = " And PODate between #" & FromDate & "# and #" & ToDate & "#"


ElseIf FromDate = "" And ToDate <> "" Then


Session("Purchase Search String") = " And PODate between #" & Date.Now.ToString("dd/MM/yyyy") & "# and #" & ToDate & "#"


End If


End If



Any help from any kind soul would be deeply appreciated!


Thanks in advance.



Cherry




Hi,


I think you need to create a dynamic query based on your filter then bind that dataset with grid view.


This link might help you.


http://www.aspdotnet-suresh.com/2011/11/how-to-filter-gridview-records-with.html





in your search method, you are only setting some session variables and not re-binding the data from db. do you not need to query the database with the user filter date values?


thanks. 



Hi Jalpesh,


That was a good suggestion thankyou! :) I guessed I missed out the part to create a dynamic query to retrieve the data associated with the date range selected.


Now that I have done it, I encountered another error where my data is not filtered according to the date after I select an item from the drop down list. Any suggestions to go about doing it? (by the way I'm using multiviews for my calendar and drop down
list controls)


Thanks in advance!


Cherry



Hi Vinz,


I have came across that article before but unfortunately it was not compatible to what I'm working on currently but thankyou anyway! :) 


Now that I have done it, I encountered another error where my data is not filtered according to the date after I select an item from the drop down list. Any suggestions to go about doing it? (by the way I'm using multiviews for my calendar and drop down
list controls)


Thanks in advance!



Hi ijaz,


Yes I did miss out that part of querying the database haha. But it's working fine now thankyou!


However, I encountered another error where my data is not filtered according to the date after I select an item from the drop down list. Any suggestions to go about doing it? (by the way I'm using multiviews for my calendar and drop down list controls)


Thanks in advance!


沒有留言:

張貼留言