In this example, we’ll learn How to filter the data entered into the textbox control between two selected dates with datetimepicker in C# and this program solve filtered datagridview problem using between two datetimepicker values.
Form Design:
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb; namespace veri_filtre { public partial class Form1 : Form { public Form1() { InitializeComponent(); } OleDbConnection con; OleDbDataAdapter da; DataTable dt; private void button2_Click(object sender, EventArgs e) { FillAllRecords(); } void FillAllRecords() { con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbService.accdb"); da = new OleDbDataAdapter("SELECT * FROM service", con); dt = new DataTable(); con.Open(); da.Fill(dt); con.Close(); dataGridView1.DataSource = dt; } void FilterRecords() { con = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=dbService.accdb"); con.Open(); string sql = "SELECT * FROM service Where p_date BETWEEN @date1 and @date2 and product=@data"; DataTable dt = new DataTable(); OleDbDataAdapter adp = new OleDbDataAdapter(sql, con); adp.SelectCommand.Parameters.AddWithValue("@date1", dateTimePicker1.Value); adp.SelectCommand.Parameters.AddWithValue("@date2", dateTimePicker2.Value); adp.SelectCommand.Parameters.AddWithValue("@data", textBox1.Text); adp.Fill(dt); con.Close(); dataGridView1.DataSource = dt; } private void button1_Click(object sender, EventArgs e) { FilterRecords(); } private void Form1_Load(object sender, EventArgs e) { FillAllRecords(); } } } |