Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How am I going to search the records saved on my DataGridView using DateTimePicker

I have a Column on my DataGrid name "EventDate" and I have used the DateTimePicker to input new records to that Column.

Now I want to do is my DataGridView will only view the records depends on the value on my DateTimePicker..
Posted
Updated 18-Mar-12 3:00am
v2
Comments
ProEnggSoft 18-Mar-12 9:00am    
Edit: Capitalization corrected and code tags added - PES

1 solution

If your dataGridView1 is bound to DataSource through bindingSource1, then use the following code in ValueChanged event of DateTimePicker control, to filter the BindingSource, to show only those records which have the date selected in the DateTimePicker.
C#
private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
        bindingSource1.Filter = string.Format("EventDate = #{0}#", dateTimePicker1.Value.ToLongDateString());
}

If you have inserted the date into database in text format, say using ToShortDateString() of DateTimePicker, then use the same text format in the filter string of bindingSource1 using ' instead of #, as below
C#
private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
    bindingSource1.Filter = string.Format("EventDate = '{0}'", dateTimePicker1.Value.ToShortDateString());
}
 
Share this answer
 
Comments
VictorTheGreat 18-Mar-12 9:29am    
sir what if i have use this to view my database in datagrid

<pre lang="vb">Private Sub showitemsM()
Dim dt As New DataTable
Dim ds As New DataSet
Dim da As New OleDbDataAdapter
Dim mSource As New BindingSource

da = New OleDbDataAdapter("Select * from Marriage", dbPath)
ds.Tables.Add(dt)
da.Fill(dt)

mSource.DataSource = dt
dgvM.DataSource = mSource

dgvM.Columns(1).Width = 200
dgvM.Columns(2).Width = 160
dgvM.Columns(3).Width = 60
dgvM.Columns(4).Width = 60
dgvM.Columns(5).Width = 190
dgvM.Columns(6).Width = 250
dgvM.Columns(7).Width = 120
dgvM.Columns(8).Width = 80
dgvM.Columns(9).Width = 40
dgvM.Columns(10).Width = 80
dgvM.Columns(11).Width = 90
dgvM.Columns(12).Width = 70
dgvM.Columns(13).Width = 80
dgvM.Columns(14).Width = 40
dgvM.Columns(15).Width = 90
dgvM.Columns(16).Width = 90
dgvM.Columns(17).Width = 70
dgvM.Columns(18).Width = 80
dgvM.Columns(19).Width = 40
dgvM.Columns(20).Width = 90
dgvM.Columns(21).Width = 90
dgvM.Columns(22).Width = 70
dgvM.Columns(0).Width = 0
dgvM.Columns(0).Visible = False
dt.DefaultView.Sort = "EventDate"

clearbindM()

txtIDm.DataBindings.Add(New Binding("Text", mSource, "ID", True, DataSourceUpdateMode.OnPropertyChanged))
</pre>

*note that showitemsM sub i also use that to bind my controls to each columns and to set the size of my columns.
ProEnggSoft 18-Mar-12 12:08pm    
Here BindingSource mSource is used. Set mSource.Filter property as shown in the solution.
VictorTheGreat 18-Mar-12 22:42pm    
sir im getting an error on the code you gave me.. i think it has something to do with my code above?

http://www.mypicx.com/uploadimg/1686533368_03182012_1.png
(check the error)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900