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 partial class trackInput : Form
{

    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$"))
        {
            con.Open();
            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 + "VALUES dsysdate = " +string.Format("{0:dd/MM/yyyy}"), 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)
    {   
        SqlCeCommand cm = new SqlCeCommand("SELECT CAST(FLOOR(CAST(dsysdate as FLOAT)) AS DateTime) FROM BRDATA");  
        bRDATABindingSource1.Filter = string.Format("dsysdate = #{0:dd/MM/yyyy}#",dateTimePicker1.Value.ToLongDateString(), cm);
    }
}

}

Recommended Answers

All 3 Replies

What type of the object "bRDATABindingSource1"?

Next, why would you filter by daytime? You mean to filter for specific time of a day (of this same day)? Or to want to filter ony by time (no matter of days)?
It would be smart idea to hav all data in datatable, and then use soelect method of it, or using DataView and using Filter method.

bRDATABindingSource1 is my binding data source instance. What I meant is to filter by date. I have an application where firstly the user can filter the datagridview by inputting ID. After the data of the ID is displayed, the user can filter it even more by selecting a date using the datetimepicker. My problem lies in the date filter.

Binding source ok, what why type? DataTable, List<T>, something else?
Its important to know for doing filtering..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.