title: C# show data between two dates in Datagridview. Dates from DatetimePicker or Textbox
body: <p>I am working on a code in C# where I used OLEDB connection string to connect MS access database. 
      I have a form where I show data from database in datagridview on some criteria.
  Below are criteria:</p>
  
  <p>a) Person (come from database in </p>
  
  <p>b) Process (come from database in textbox)</p>
  
  <p>c) From Date (Datetimepicker)</p>
  
  <p>d) To Date (Datetimepicker)</p>
  
  <p>Result what I want: first I select person and than process and than From Date and then To date and click on View Button. which should show data from MS-Access based on above criteria I selected.</p>
  
  <p>I've tried below methods:</p>
  
  <p><strong>Code:</strong>
      1. For Person and Process filter:</p>
  
  <pre><code>        DataView DV = new DataView(dt1);
          DV.RowFilter = string.Format("[Person] LIKE '%{0}%'", textBox5.Text);
          dataGridView1.DataSource = DV;
  </code></pre>
  
  <ol start="2">
  <li>For Date Time between Two date I tried many and Google lots but not find answer. I tried below:</li>
  </ol>
  
  <p>a) </p>
  
  <pre><code>        SqlConnection con = new 
          SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
          string queryString = "";
          queryString = "SELECT * FROM Table1 WHERE dob BETWEEN @startdate AND @enddate";
          System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(queryString, con);
          sqlCmd.Parameters.Add("@startdate", System.Data.SqlDbType.Date).Value = textBox7.Text;
          sqlCmd.Parameters.Add("@enddate", System.Data.SqlDbType.Date).Value = textBox8.Text;
          System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
          System.Data.DataSet dataSet = new System.Data.DataSet();
          dataAdapter.Fill(dataSet);
          GridView1.DataSource = dataSet;
          GridView1.DataBind();
  </code></pre>
  
  <p>b)</p>
  
  <pre><code>        string FD = "";
          FD = dateTimePicker4.Value.ToString("dd-MM-yyyy");
          string TD = "";
          TD = dateTimePicker5.Value.ToString("dd-MM-yyyy");
          connection.Close();
          connection.Open();
          OleDbCommand command123 = new OleDbCommand();
          command123.Connection = connection;
          string query123 = "select * from Table1 where [P Date] between date '"# + dateTimePicker4.Text.ToString() + #"' and date '"# + dateTimePicker5.Text.ToString() + #"'"
          command123.CommandText = query123;
          OleDbDataAdapter da123 = new OleDbDataAdapter(command123);
          DataTable dt123 = new DataTable();
          da123.Fill(dt123);
          dataGridView1.DataSource = dt123;
  </code></pre>
  
  <p>c)</p>
  
  <pre><code>        DataTable dt = new DataTable();
          da.Fill(dt);
          DataView DV = new DataView(dt1);
          DV.RowFilter = string.Format("[P Date] &gt;=" + textBox7.Text + " and &lt;" + textBox8.Text + "");
          dataGridView1.DataSource = DV;
  </code></pre>
  
  <p>d)</p>
  
  <pre><code>        DataTable dt1 = new DataTable();
          DataView DV = new DataView(dt1);
          //DV.RowFilter = "[P Date] IN (#11/01/2019#, #11/11/2019#)";
          //DV.RowFilter = "[P Date] &gt;=#"+dateTimePicker4.Text+"# and [P Date] &lt;=#"+dateTimePicker5.Text+"#";
          //dataView.RowFilter = "Date IN (#12/31/2008#, #1/1/2009#)" // date time values
          //DV.RowFilter ="([P Date] &gt;=CDate('dateTimePicker4.Text')) and ([P Date] &lt;=CDate('dateTimePicker5.Text'))";
          //DV.RowFilter = string.Format(CultureInfo.InvariantCulture.DateTimeFormat, "([P Date]&gt;=#{dateTimePicker4.text}#) and ([P Date] &lt;=#{dateTimePicker5.Text}#)");
          dataGridView1.DataSource = DV;
  
  For connection I am trying OLEDB connection.
  
              connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DatabasePath.accdb;
              Jet OLEDB:Database Password=password";
          connection.Close();
          connection.Open();
          OleDbCommand command1 = new OleDbCommand();
          command1.Connection = connection;
          //Select all column use belw query
          string query = "select * from Table1";
          command1.CommandText = query;
          OleDbDataAdapter da = new OleDbDataAdapter(command1);
          DataTable dt = new DataTable();
          da.Fill(dt);
          dataGridView1.DataSource = dt;
          connection.Close();
  
  this code is working but problem is that it shows all data not based on criteria based data.
  
  I expect the output in Datagridview based on all criteria. Show only data which falls under all criteria.
  </code></pre>