Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How I filter date to date data ned show the result in Datagridview C#

$
0
0

Hi!

In my code I have button to filter excel files data "date to date" in datagridview. I want to load filtered data when I tryck other button, but load button show me all data in excel files, what should I do to load and show only filtered data in datagridview when I tryck load button. I mean it is work when I tryck only filter"date to date" button, but when I tryck load button it will show me all data. How I keep the filtered data when I tryck load button. Here is the code:

//======================Filter date to date=======================
private void Button3_Click(object sender, EventArgs e) {

 try {
  DataTable dataTable = new DataTable();
  System.Windows.Forms.ComboBox.ObjectCollection items = drop_down_sheet.Items;
  foreach(var item in items) {

   MyClass myClass = (MyClass) item;

   string constr = string.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + myClass.Path + ";Extended Properties = \"Excel 12.0; HDR=Yes;\"; ");
   OleDbConnection con = new OleDbConnection(constr);
   OleDbCommand command = new OleDbCommand("Select * From[" + myClass.TableName + "]", con);
   OleDbDataAdapter da = new OleDbDataAdapter(command);

   DataTable dt = new DataTable();
   da.Fill(dt);
   dataTable.Merge(dt);


  }
  string filter = "Datum >= '" + startDate.Value.ToString("yyyy-MM-dd") + "' AND Datum <= '" + endDate.Value.ToString("yyyy-MM-dd") + "'";
  DataRow[] filteredRows = dataTable.Select(filter);

  dataGridView1.DataSource = filteredRows.CopyToDataTable();

 } catch (Exception ex) {
  MessageBox.Show(ex.Message,
   "Important Note",
   MessageBoxButtons.OK,
   MessageBoxIcon.Error,
   MessageBoxDefaultButton.Button1);

 }
}
//===========================Load Data====================================
private void Button5_Click(object sender, EventArgs e) {

 try {

  System.Windows.Forms.ComboBox.ObjectCollection items = drop_down_sheet.Items;
  foreach(var item in items) {
   MyClass myClass = (MyClass) item;
   string constr = string.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + myClass.Path + ";Extended Properties = \"Excel 12.0; HDR=Yes;\"; ");
   OleDbConnection con = new OleDbConnection(constr);
   OleDbCommand command = new OleDbCommand("Select * From[" + myClass.TableName + "]", con);
   OleDbDataAdapter da = new OleDbDataAdapter(command);

   da.Fill(dataTable);

   foreach(DataRow row in dataTable.Rows) {
    dataGridView1.DataSource = dataTable;
   }

  }

 } catch (Exception ex) {
  MessageBox.Show(ex.Message,
   "Important Note",
   MessageBoxButtons.OK,
   MessageBoxIcon.Error,
   MessageBoxDefaultButton.Button1);

 }
}

can you help me please!


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>