M.Heart
M.Heart

Reputation: 101

Filter datagridview using datetimepicker?

I'm doing an application where there will be two filters.

First filter is when user will enter an ID then only the data of that ID is displayed. I've managed to done that but the problem is on the second filter I try to implement. After the user enter the ID then display the ID data, then the user can filter that data even more based dates. So I try using datetimepicker tools.

But when I choose the date, mydatagridview won't filter to show only the chosen date data. It still shows all the data from the first ID filter.

Any help will greatly appreciated.

Here is my code :

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];
                }
                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];
            }
            dataGridView1.DataSource = bRDATABindingSource1;
            con.Close();
        }
    }

    private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {  
       bRDATABindingSource1.Filter = string.Format("dsysdate = #{0}#", dateTimePicker1.Value.ToLongDateString());

    }
}

}

EDITED

Upvotes: 0

Views: 2950

Answers (1)

Kirin Yao
Kirin Yao

Reputation: 1636

I'm afraid the DataSource of dataGridView1 is local variable dt in both your events. But you're likely trying to filter an instance field bRDATABindingSource1. There is no more context information for me. Are you sure this is correct?

Try to use bRDATABindingSource1 instead of dt, and see if that works.

Upvotes: 1

Related Questions