使用日期筛选datagridview

问题描述:

我想通过2 datetimepickers - startDate和endDate筛选datagridview中的截止日期列。使用日期筛选datagridview

datagridview的是TaskTable2, datetimepicker1是startSchedule, datetimepicker2是endSchedule和 限期datagridview的是deadlineRow

到目前为止,我已经得到了下面的代码,成功使无形的行不属于所选择的开始之间和结束日期。

private void scheduleButton_Click(object sender, EventArgs e) 
    { 

     DateTime startSchedule = startDate.Value.Date; 
     DateTime endSchedule = endDate.Value.Date; 

     if (startSchedule <= endSchedule)// runs foreach loop if startdate and enddate are valid 
     { 
      foreach (DataGridViewRow dr in TaskTable2.Rows)// loops through rows of datagridview 
      { 
       string deadline = dr.Cells["Deadline"].Value.ToString(); // gets deadline values 
       DateTime deadlineRow = Convert.ToDateTime(deadline); // converts deadline string to datetime and stores in deadlineRow variable 

       if (startSchedule <= deadlineRow && deadlineRow <= endSchedule) // filters deadlines that are => startDate and <= endDate 
       { 
        dr.Visible = true; // display filtered rows here. 
       } 
       else 
       { 
        dr.Visible = false; // hide rows that are not beteen start and end date. 
       } 

      } 
     } 
     else 
     {  
      MessageBox.Show("Please ensure Start Date is set before End Date."); // ensures user selects an end date after the start date. 
     } 
    } 

但是,我有几个存在的问题:

  1. 的应用程序崩溃,并且我收到以下错误,当我选择将不显示任务的日期范围:

“与货币经理的仓位相关联的行不能被隐藏'

  1. 我有一个pri nt按钮,应该打印过滤结果。 但是,它打印存储在datagridview中的所有数据,即使某些行可见=按下日程表按钮时也是false,所以我猜测我需要使用不同的方法来删除行而不是隐藏它们。

datagridview绑定到一个XML文件,因此可以从datagridview中删除数据以进行过滤和打印,只要它们保留在XML文件中即可。

任何帮助将不胜感激!

三江源

我会用在bindingsourceFilter属性为datagridviewFilter属性允许您查看DataSource的子集。从MSDN

实施例:

private void PopulateDataViewAndFilter() 
{ 
    DataSet set1 = new DataSet(); 

    // Some xml data to populate the DataSet with. 
    string musicXml = 
     "<?xml version='1.0' encoding='UTF-8'?>" + 
     "<music>" + 
     "<recording><artist>Coldplay</artist><cd>X&amp;Y</cd></recording>" + 
     "<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" + 
     "<recording><artist>Dave Matthews</artist><cd>Live at Red Rocks</cd></recording>" + 
     "<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" + 
     "<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" + 
     "</music>"; 

    // Read the xml. 
    StringReader reader = new StringReader(musicXml); 
    set1.ReadXml(reader); 

    // Get a DataView of the table contained in the dataset. 
    DataTableCollection tables = set1.Tables; 
    DataView view1 = new DataView(tables[0]); 

    // Create a DataGridView control and add it to the form. 
    DataGridView datagridview1 = new DataGridView(); 
    datagridview1.AutoGenerateColumns = true; 
    this.Controls.Add(datagridview1); 

    // Create a BindingSource and set its DataSource property to 
    // the DataView. 
    BindingSource source1 = new BindingSource(); 
    source1.DataSource = view1; 

    // Set the data source for the DataGridView. 
    datagridview1.DataSource = source1; 

    //The Filter string can include Boolean expressions. 
    source1.Filter = "artist = 'Dave Matthews' OR cd = 'Tigerlily'"; 
} 

我使用的Filter这种类型的显示根据帐户数据。对于帐户,当用户输入帐户号码时,我有一个文本框,并使用TextChanged事件来应用过滤器。然后我有一个按钮用于从绑定源中删除过滤器。

如果你想通过日期筛选,你可以按照本SO问题的说明:

BindingSource Filter by date

上是不存在的日期使用的过滤器应该不会崩溃的应用程序,它只会显示任何内容。

找到了解决异常这里: http://discuss.itacumens.com/index.php?topic=16375.0

我将此添加到我的代码直接之前,我尝试设置的行是不可见的。 row是我的ForEach循环变量。我检查它是否被选中,并且在设置visible属性之前尝试清除行和单元格选择。

  If gridItems.SelectedRows.Count > 0 AndAlso row.Index = gridItems.SelectedRows(0).Index Then 
       'fixes dumb exception with row.visible = false 
       gridItems.ClearSelection() 
       gridItems.CurrentCell = Nothing 
      End If 

看来问题在于使当前行或单元格不可见。

+0

看起来你甚至不需要先清除选择;只需将CurrentCell设置为空/没有任何操作对我来说是个窍门。谢谢你的提示! – 2012-04-04 00:10:19

我遇到了与例外情况完全相同的问题'与货币经理的位置相关联的行不能被隐藏'。

dgridView.CurrentCell = null; 
dgridView.Rows[i].Visible = false; 

只要将CurrentCell设置为null就可以为我修复它。如果它破坏了某些东西,我还没有进一步检查。