Question Filtering a BindingSOurce and Datagridview by Date

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
159
Programming Experience
1-3
Hi,

I am trying to filter a binding source by a date field in a datagridview (using vb.net 2010 and sql server 2008)

Now my filter works for names no problem but not for dates ! Dates in the database are proper dates in the yyyy/mm/dd format !

Heres my code I followed an example on the web, but cant find any other examples that might help?

bs1 is the binding source ! No error is produced though!


Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
'Search by Client ID
Dim Ans As String

Try
Ans = InputBox("Search For DOB..(format.31/12/1961", "Search", "", 100, 100)

If Ans = "" Then
MsgBox("No Date entered")
Else
bs1.Filter = ("DOB = #' & Ans & '#")
DataGridView1.DataSource = bs.DataSource
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")


End Try
End Sub

Help !
 
Last edited:
Dates in the database are proper dates in the yyyy/mm/dd format !

If they are proper dates then they have no format. Dates are just stored as numbers. It's only when you display a date as text that format becomes relevant.

You should not be using InputBox if you want the user to enter a date. You should not be using InputBox for anything to be frank. It's garbage. If you've been told to use it for homework then you're stuck with it but otherwise you should be creating your own dialogue with a DateTimePicker. That way you've got a Date so you know it's valid.

Your InputBox prompt is wrong. You're telling the user what format to use and you're specifying the wrong format. If you've read the appropriate documentation then you know that you need to use #M/dd/yyyy# when specifying a date in the Filter property of a BindingSource.
 
Thanks for the advice John, I suppose a modal form with a date picker, using showdialog() would work !

Can you give me an example of how to best create my own dialog

Thanks again...
 
Can you give me an example of how to best create my own dialog
What do you not understand about the examples you found when you searched the web for yourself?
 
All the examples I found used an inputbox !

I look for some with a custom dialogform

That's because you're looking for the wrong examples. So many people have a warped idea of what an example actually is. It's not something that you can copy and paste and do exactly what you want. If you're looking for examples of using user input then how you get that user input is not relevant, so they're not going to go to lots of trouble creating a custom form, etc, etc. They're going to do it the easiest way that everyone can use because that's not actually the point of the example. If you want examples of using a custom dialogue to gather user input then you need to look for examples of that separately. You then need to apply the principles exemplified and apply them to your specific set of circumstances for yourself.
 
Back
Top