简体   繁体   English

使用datetimepicker过滤datagridview?

[英]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. 第一个过滤器是用户输入ID仅显示该ID的数据。 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. 用户输入ID之后,显示ID数据,然后用户可以根据更多日期过滤该数据。 So I try using datetimepicker tools. 所以我尝试使用datetimepicker工具。

But when I choose the date, mydatagridview won't filter to show only the chosen date data. 但是,当我选择日期时, mydatagridview不会过滤以仅显示所选日期数据。 It still shows all the data from the first ID filter. 它仍然显示来自第一个ID过滤器的所有数据。

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 已编辑

I'm afraid the DataSource of dataGridView1 is local variable dt in both your events. 恐怕两个事件中dataGridView1DataSource都是局部变量dt But you're likely trying to filter an instance field bRDATABindingSource1 . 但是您可能试图过滤实例字段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. 尝试使用bRDATABindingSource1代替dt ,看看是否bRDATABindingSource1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM