使用datetimepicker过滤datagridview? [英] Filter datagridview using datetimepicker?

查看:59
本文介绍了使用datetimepicker过滤datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个有两个过滤器的应用程序.第一个过滤器是当用户输入ID时,仅显示该ID的数据.我已经做到了,但是问题出在我尝试实现的第二个过滤器上.用户输入ID之后,显示ID数据,然后用户可以根据更多日期过滤该数据.因此,我尝试使用datetimepicker工具.但是,当我选择日期时,mydatagridview不会过滤以仅显示所选日期数据.它仍然显示来自第一个ID过滤器的所有数据.任何帮助将不胜感激.这是我的代码:

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 :

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());
        }
    }
}

推荐答案

")) { // string [] id = textBox1.Text.Split('\ n'); 字符串过滤器= " + System.Text.RegularExpressions.Regex.Replace(textBox1.Text.Trim(), @" 'OR conno ='")+ " ; // 字符串Ids2 ="cmpsno ='" + System.Text.RegularExpressions.Regex.Replace(textBox2.Text.Trim (),@"\ s * \ n \ s *",'或cmpsno ='")+'"; con.Open(); SqlCeCommand com = SqlCeCommand(" + Ids,con); // SqlCeCommand com2 = new SqlCeCommand("SELECT conno,cmpsno,ctrx,dsysdate,cstnno,从BRDATA WHERE出发的地方" + Ids2,con); SqlCeDataAdapter adap = SqlCeDataAdapter(com); // SqlCeDataAdapter adap2 = new SqlCeDataAdapter(com2); DataSet set = new DataSet(); adap.Fill( set ); 如果(设置.Tables.Count> 0 ) { dt = set .Tables[ 0 ]; } dataGridView1.DataSource = dt; con.Close(); } } 私有 无效 trackMPSbtn_Click(对象发​​件人,EventArgs e) { dataGridView1.Visible = true ; DataTable dt = ; // 字符串connoInput = textBox1.Text; 字符串 conString = Properties.Settings.Default.BMSDATAConnectionString; 使用(SqlCeConnection con = SqlCeConnection( @" 数据源= C:\ Documents and Settings \ Administrator \ My Documents \ Visual Studio 2008 \ Projects \ TrackCon \ TrackCon \ BMSDATA.sdf; Persist安全信息= True;密码= 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'); 字符串过滤器= " + System.Text.RegularExpressions.Regex.Replace(textBox2.Text.Trim(), @" '或cmpsno ='")+ " ; con.Open(); SqlCeCommand com = SqlCeCommand(" + IDs,con); SqlCeDataAdapter adap = SqlCeDataAdapter(com); DataSet set = new DataSet(); adap.Fill( set ); 如果(设置.Tables.Count> 0 ) { dt = set .Tables[ 0 ]; } dataGridView1.DataSource = dt; con.Close(); } } // 我的日期选择器代码 私有 无效 dateTimePicker1_ValueChanged(对象发​​件人,EventArgs e) { bRDATABindingSource1.Filter = 字符串 .Format(" ,dateTimePicker1.Value.ToLongDateString()); } } }
")) { //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()); } } }


从您的代码中看到的

As seen from your code

dataGridView1.DataSource = dt;

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



DataGridView.DataSource属性设置为DataTable dt,其中将过滤器设置为BindingSource,因此该过滤器不应用于DataGridView.

将DataGridView的DataSource 属性设置为bRDATABindingSource1.



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.


这篇关于使用datetimepicker过滤datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆