Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm doing an application where there will be two filter. 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 chose 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 :
C#
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.SqlServerCe;


namespace TrackCon
{
    public partial class trackInput : Form
    {
        public trackInput()
        {
            InitializeComponent();
            dataGridView1.Visible = false;
        }

        /*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;
            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[] id = textBox1.Text.Split('\n');
                    string filter = "conno= '" + System.Text.RegularExpressions.Regex.Replace(textBox1.Text.Trim(), @"\s*\n\s*", "' OR conno= '") + "'";
                    //string Ids2 = "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);
                    //SqlCeCommand com2 = new SqlCeCommand("SELECT conno,cmpsno,ctrx,dsysdate,cstnno,corigin FROM BRDATA WHERE " + Ids2, con);
                    SqlCeDataAdapter adap = new SqlCeDataAdapter(com);
                    //SqlCeDataAdapter adap2 = new SqlCeDataAdapter(com2);
                    DataSet set = new DataSet();
                    adap.Fill(set);
                    if (set.Tables.Count > 0)
                    {
                        dt = set.Tables[0];
                    }
                    dataGridView1.DataSource = dt;
                    con.Close();
            }
        }

        private void trackMPSbtn_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = true;
            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[] id = textBox1.Text.Split('\n');
                string filter = "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)
                {
                    dt = set.Tables[0];
                }
                dataGridView1.DataSource = dt;
                con.Close();
            }
        }
        
        //my datetimepicker code
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
           bRDATABindingSource1.Filter = string.Format("dsysdate = #{0}#", dateTimePicker1.Value.ToLongDateString());
        }
    }
}
Posted

1 solution

As seen from your code

C#
dataGridView1.DataSource = dt;

bRDATABindingSource1.Filter = string.Format("dsysdate = #{0}#", dateTimePicker1.Value.ToLongDateString());


The DataGridView.DataSource property is set the DataTable dt, where as the filter is set to the BindingSource, so the filter is not applied to the DataGridView.

Set the DataSource property of the DataGridView to the bRDATABindingSource1.
 
Share this answer
 
Comments
nizam15 27-Mar-12 0:04am    
if I change it then when I click the button and run the first filter, no data will be displayed in the gridview.
ProEnggSoft 27-Mar-12 3:24am    
Set
bRDATABindingSource1.DataSource = dt;
then it will work
nizam15 27-Mar-12 3:32am    
thanks man!!:D
ProEnggSoft 27-Mar-12 3:36am    
Happy to know that the solution worked for you :)
nizam15 27-Mar-12 3:39am    
One more thing, I suspect there is a problem with my datepicker filter because mine can only filter data which have dates only. Most of my data is a datetime, hence it cannot detect those data that contain datetime.

I'll try to modified

string.Format("dsysdate = #{0}#", dateTimePicker1.Value.ToLongDateString());

but all of it turns out with errors.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900