filterng 2 textBOxes and 2 dataPickers together?

Oct 9 2014 10:12 AM
Hello,
 
 
i have a little problem, i want to filtering by user, event(from textBoxes) and from range of dates(2 dateTimePickers).
I write code, that work alone, but how to bind together? If write user and event and range of dates, in dataGridView show me result?
This is my code
 

[code]

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.OleDb;

namespace zadatak2

{

public partial class Izvestaj : Form

{

private OleDbConnection connection = new OleDbConnection();

private BindingSource bs;

public Izvestaj()

{

InitializeComponent();

}

OleDbDataAdapter sda;

DataTable dt;

private void Izvestaj_Load(object sender, EventArgs e)

{

connection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\base.mdb;User Id=admin;Password=;";

dt = new DataTable();

sda = new OleDbDataAdapter("SELECT users.user,events.event,Tomislav.Date FROM events INNER JOIN (users INNER JOIN Tomislav ON users.ID = Tomislav.user_ID) ON events.ID = Tomislav.event_ID;", connection);

sda.Fill(dt);

dataGridView1.DataSource = dt;

dataGridView1.Columns[0].Width = 120;

dataGridView1.Columns[1].Width = 150;

dataGridView1.Columns[2].Width = 150;

bs = new BindingSource(dt,null);

}

private void textBox3_TextChanged(object sender, EventArgs e)

{

if (textBox3.Text == string.Empty)

{

bs.RemoveFilter();

}

else

{

bs.Filter = string.Format("event LIKE '*{0}*'", textBox3.Text);

}

}

private void textBox1_TextChanged(object sender, EventArgs e)

{

if (textBox1.Text == string.Empty)

{

bs.RemoveFilter();

}

else

{

bs.Filter = string.Format("user LIKE '*{0}*'", textBox1.Text);

}

}

private void button1_Click(object sender, EventArgs e)

{

if(dateTimePicker1.Value>dateTimePicker2.Value)

{

MessageBox.Show("That’s not possible. Please select another range.", "Range date", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

else{

string query = "select users.user, events.event, Date from users, events, Tomislav where Date BETWEEN ? AND ?";

OleDbCommand cmd = new OleDbCommand(query, connection);

cmd.Parameters.AddWithValue("SDate", DbType.DateTime).Value = dateTimePicker1.Value;

cmd.Parameters.AddWithValue("EDate", DbType.DateTime).Value = dateTimePicker2.Value;

DataSet ds = new DataSet();

sda= new OleDbDataAdapter();

connection.Open();

sda.SelectCommand = cmd;

sda.Fill(ds, "Tomislav");

dataGridView1.DataSource = ds.Tables[0];

connection.Close();

}

}

private void unosUBazuToolStripMenuItem_Click(object sender, EventArgs e)

{

this.Hide();

Form1 baza = new Form1();

baza.ShowDialog();

}

private void exitToolStripMenuItem1_Click(object sender, EventArgs e)

{

if (MessageBox.Show("Are you sure?", "Exit", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)

{

Application.Exit();

}

}

}

}

[/code]


Answers (1)