2012-03-27 129 views
0

我正在做一个应用程序,其中会有两个过滤器。使用datetimepicker过滤datagridview?

第一个过滤器是当用户输入一个ID,那么只显示那个数据ID。我设法做到了,但问题出在我试图实现的第二个过滤器上。用户输入ID后会显示ID数据,然后用户可以过滤该数据甚至更多的日期。所以我尝试使用datetimepicker工具。

但是,当我选择日期时,mydatagridview将不会筛选以仅显示所选日期数据。它仍然显示来自第一个ID过滤器的所有数据。

任何帮助将不胜感激。

这里是我的代码:

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

+1

Metro?的WinForms? WPF? Silverlight的? ASP.Net? MonoTouch的? – SLaks 2012-03-27 03:57:25

+0

抱歉不提。它在WinForms中。 – 2012-03-27 04:07:18

回答

1

我怕dataGridView1DataSource是在这两个事件的局部变量dt。但是您可能试图过滤实例字段bRDATABindingSource1。我没有更多的上下文信息。你确定这是正确的吗?

尝试使用bRDATABindingSource1而不是dt,看看是否有效。

+0

我相信你的分析是正确的。如果dataGridView1的数据源设置为bRDATABindingSource1并且未在代码中更改,并且对数据源所做的更改已转换为bRDATABindingSource1而不是本地变量,则此代码可能会按预期工作。 – 2012-03-27 05:10:21

+0

我已将代码更改为 dataGridview1.DataSource = bRDATABindingSource1; 但它不会在gridview中显示任何数据。尽管我已经在设计视图中以及在我的代码中为bRDATABindingSource1设置了我的gridview的数据源。 – 2012-03-27 05:17:36

+0

@ shahrul1509我想你还没有设置你的'bRDATABindingSource1'。试试'bRDATABindingSource1.DataSource = set.DataTables [0];'然后'dataGridView1.DataSource = bRDATABindingSource1;'。当然有更优雅的方法,但你应该首先了解这是否是问题。 – 2012-03-27 06:02:55