2017-06-27 100 views
0

我有一個連接到充滿數據的BindingSource的DGrid。我正在嘗試使用複選框爲用戶過濾出名爲「狀態」的特定列中的數據。我有四個複選框,因爲同一列中有四個不同的數據文本(列在最右側)。 Picture hereBindingSource過濾器 - 從字符串中刪除單詞

很顯然,當用戶選中/取消選中其中一個複選框時,它會設置一個過濾器到網格,以添加/刪除列中的數據。

這裏是我的代碼,當複選框被選中的作品:

private void chkProgress_CheckedChanged(object sender, EventArgs e) 
    { 
     string filter = ""; 

     if (chkProgress.Checked) 
     { 

      filter = "Status LIKE '%Progress%'"; 

      if (chkPending.Checked) 
      { 
       filter += " Or Status LIKE '%Pending%'"; 
      } 
      else 
      { 

      } 

      if (chkCompleted.Checked) 
      { 
       filter += " Or Status LIKE '%Completed%'"; 
      } 
      else 
      { 

      } 

      if (chkArchived.Checked) 
      { 
       filter += " Or Status LIKE '%Archived%'"; 
      } 
      else 
      { 

      } 
     } 
     else 
     { 



     } 

     MessageBox.Show(filter); 
     bs.Filter = string.Format(filter); 
     refreshGrid(); 

    } 

    private void chkCompleted_CheckedChanged(object sender, EventArgs e) 
    { 
     string filter = ""; 

     if (chkCompleted.Checked) 
     { 

      filter = "Status LIKE '%Completed%'"; 

      if (chkPending.Checked) 
      { 
       filter += " Or Status LIKE '%Pending%'"; 
      } 
      else 
      { 

      } 

      if (chkProgress.Checked) 
      { 
       filter += " Or Status LIKE '%Progress%'"; 
      } 
      else 
      { 

      } 

      if (chkArchived.Checked) 
      { 
       filter += " Or Status LIKE '%Archived%'"; 
      } 
      else 
      { 

      } 
     } 
     else 
     { 



     } 

     MessageBox.Show(filter); 
     bs.Filter = string.Format(filter); 
     refreshGrid(); 
    } 

    private void chkArchived_CheckedChanged(object sender, EventArgs e) 
    { 
     if (chkArchived.Checked) 
     { 

     } 
     else 
     { 

     } 
    } 

    private void chkPending_CheckedChanged(object sender, EventArgs e) 
    { 

     string filter = ""; 

     if (chkPending.Checked) 
     { 

      filter = "Status LIKE '%Pending%'"; 

      if(chkProgress.Checked) 
      { 
       filter += " Or Status LIKE '%Progress%'"; 
      } 
      else 
      { 

      } 

      if(chkCompleted.Checked) 
      { 
       filter += " Or Status LIKE '%Completed%'"; 
      } 
      else 
      { 

      } 

      if(chkArchived.Checked) 
      { 
       filter += " Or Status LIKE '%Archived%'"; 
      } 
      else 
      { 

      } 
     } 
     else 
     { 


     } 

     MessageBox.Show(filter); 
     bs.Filter = string.Format(filter); 
     refreshGrid(); 
    } 

我怎麼會去從字符串過濾器刪除數據的時候都選中?類似於 - =?的東西

回答

0

這個怎麼樣:

if (chkPending.Checked) 
{ 
    filter += " Or Status LIKE '%Pending%'"; 
} 
else 
{ 
    filter = filter.Replace(" Or Status LIKE '%Pending%'", ""); 
} 
+0

乾杯我已經使用這個看到我更新的問題。但有時它似乎並沒有將字符串替換爲無。它是間歇性的,每6次嘗試就不會有1次。 – Zack

+0

嘗試在替換行上添加一個斷點,然後觀察filter-var,直到它無法按預期工作。 –