2012-03-13 158 views
3

我想通過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文件中即可。

任何幫助將不勝感激!

三江源

回答

3

我會用在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

上是不存在的日期使用的過濾器應該不會崩潰的應用程序,它只會顯示任何內容。

2

找到了解決異常這裏: 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

1

我遇到了與例外情況完全相同的問題'與貨幣經理的位置相關聯的行不能被隱藏'。

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

只要將CurrentCell設置爲null就可以爲我修復它。如果它破壞了某些東西,我還沒有進一步檢查。