I have an application that can filter a datagridview based on date using a datetimepicker. The “date” column in my database is a datetime data type, hence it will contain a date and time stored within but there is some data which have only dates. My problem is my datetimepicker filter can only filter those data with date = 12:00:00 AM. Those data which contains time other than that cannot be filtered when I chose the date using datatimepicker. I don’t know what’s the problem. Here is my code :
public trackInput()
{
InitializeComponent();
dataGridView1.Visible = false;
webBrowser1.Location = new Point(12, 141);
}
/*private void trackInput_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'trackingBMSDATADataSet.BRDATA' table. You can move, or remove it, as needed.
this.bRDATATableAdapter.Fill(this.trackingBMSDATADataSet.BRDATA);
}*/
private void trackBtn_Click(object sender, EventArgs e)
{
dataGridView1.Visible = true;
if (dataGridView1.Visible == true)
{
webBrowser1.Location = new Point(12, 397);
}
//DataTable dt = null;
string connoInput = textBox1.Text;
string conString = Properties.Settings.Default.BMSDATAConnectionString;
using (SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\TrackCon\TrackCon\BMSDATA.sdf;Persist Security Info = True;Password=Gdex123$"))
{
string Ids = "conno= '" + System.Text.RegularExpressions.Regex.Replace(textBox1.Text.Trim(), @"\s*\n\s*", "' OR conno= '") + "'";
SqlCeCommand com = new SqlCeCommand("SELECT conno,cmpsno,ctrx,dsysdate,cstnno,corigin FROM BRDATA WHERE " +Ids, con);
SqlCeDataAdapter adap = new SqlCeDataAdapter(com);
DataSet set = new DataSet();
adap.Fill(set);
if (set.Tables.Count > 0)
{
bRDATABindingSource1.DataSource = set.Tables[0];
}
bRDATABindingSource1.Filter = null;
dataGridView1.DataSource = bRDATABindingSource1;
con.Close();
}
}
private void trackMPSbtn_Click(object sender, EventArgs e)
{
dataGridView1.Visible = true;
if (dataGridView1.Visible == true)
{
webBrowser1.Location = new Point(12, 397);
}
//DataTable dt = null;
//string connoInput = textBox1.Text;
string conString = Properties.Settings.Default.BMSDATAConnectionString;
using (SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\TrackCon\TrackCon\BMSDATA.sdf;Persist Security Info = True;Password=Gdex123$"))
{
dataGridView1.DataSource = bRDATABindingSource1;
string Ids = "cmpsno= '" + System.Text.RegularExpressions.Regex.Replace(textBox2.Text.Trim(), @"\s*\n\s*", "' OR cmpsno= '") + "'";
con.Open();
SqlCeCommand com = new SqlCeCommand("SELECT conno,cmpsno,ctrx,dsysdate,cstnno,corigin FROM BRDATA WHERE " + Ids, con);
SqlCeDataAdapter adap = new SqlCeDataAdapter(com);
DataSet set = new DataSet();
adap.Fill(set);
if (set.Tables.Count > 0)
{
bRDATABindingSource1.DataSource = set.Tables[0];
}
bRDATABindingSource1.Filter = null;
dataGridView1.DataSource = bRDATABindingSource1;
con.Close();
}
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
bRDATABindingSource1.Filter = string.Format("dsysdate = #{0:d/M/yyyy HH:mm tt}#", dateTimePicker1.Value.ToLongDateString());
}
DateTimePicker has a property Value that contains selected date. You should use this date to form a query looking like this:
where .Date returns only date portion of DateTime. To prepare where clause, replace where part of SqlCeCommand with
Add two parameters to SqlCeCommand
And do similar exercise with cmpsno.
UPDATE: I completely missed last method.
An attempt at explanation:
As dsysdate has a time component you need to filter an interval starting at midnight and stretching to midnight of next day – this is Value.AddDays(1). Unfortunately I don’t know much about filtering BindingSource because I mostly filter at database level, hence first code. You might want to rewrite dateTimePicker1_ValueChanged to retrieve data from database instead of filtering in-memory; it would pay off when working with larger tables because of indexes.
Speaking of that, you might consider structuring your code differently. Such a task is usually done by a method that checks filtering controls, builds query and executes it, thus eliminating duplicated code and enabling you to filter by several criteria at once. Method than gets called when contents of filtering control changes.