2014-05-07 42 views
2

大家好優化綁定的DataGridView搜索功能

我有我的代碼運行完全正常,但它看起來很討厭。其目的是從我的數據庫中搜索和顯示符合搜索條件的工作人員。我的表格上填寫了一個DataGridView,填寫了我的數據庫中的工人數據。我按Search按鈕後,我只想顯示符合TextBoxes/ComboBoxes標準的工作人員列表。如果它們是空的,它會再次顯示完整列表。在形式


搜索功能:

Search feature

代碼:

與代碼
private void btnSearch_Click(object sender, EventArgs e) 
{ 
    FillList(); 
    List<Worker> list = new List<Worker>(); 
    bool match = false; 

    foreach (var worker in workerBindingSource) 
    { 
     if (txtName.Text.Length > 0 && !((Worker)worker).Name.ToLowerInvariant().Contains(txtName.Text.ToLowerInvariant())) 
     { 
      match = false; 
      continue; 
     } 
     else 
      match = true; 

     if (txtLastName.Text.Length > 0 && !((Worker)worker).LastName.ToLowerInvariant().Contains(txtLastName.Text.ToLowerInvariant())) 
     { 
      match = false; 
      continue; 
     } 
     else 
      match = true; 

     if (txtOIB.Text.Length > 0 && !((Worker)worker).OIB.ToLowerInvariant().Contains(txtOIB.Text.ToLowerInvariant())) 
     { 
      match = false; 
      continue; 
     } 
     else 
      match = true; 

     if (txtLocation.Text.Length > 0 && !((Worker)worker).Location.ToLowerInvariant().Contains(txtLocation.Text.ToLowerInvariant())) 
     { 
      match = false; 
      continue; 
     } 
     else 
      match = true; 

     if (txtAddress.Text.Length > 0 && !((Worker)worker).Address.ToLowerInvariant().Contains(txtAddress.Text.ToLowerInvariant())) 
     { 
      match = false; 
      continue; 
     } 
     else 
      match = true; 

     if (txtPhoneNumber.Text.Length > 0 && !((Worker)worker).PhoneNumber.ToLowerInvariant().Contains(txtPhoneNumber.Text.ToLowerInvariant())) 
     { 
      match = false; 
      continue; 
     } 
     else 
      match = true; 

     if (!cboProfessionalQualification.SelectedValue.ToString().Equals("Empty") && !((Worker)worker).ProfessionalQualification.ToString().ToLowerInvariant().Contains(cboProfessionalQualification.SelectedValue.ToString().ToLowerInvariant())) 
     { 
      match = false; 
      continue; 
     } 
     else 
      match = true; 

     if (!cboDegree.SelectedValue.ToString().Equals("Empty") && !((Worker)worker).Degree.ToString().ToLowerInvariant().Equals(cboDegree.SelectedValue.ToString().ToLowerInvariant())) 
     { 
      match = false; 
      continue; 
     } 
     else 
      match = true; 

     if (match) 
     { 
      list.Add(WorkerDAO.ReadWorker(((Worker)worker).ID)); 
     } 
    } 

    SortableBindingList<Worker> sortableList = new SortableBindingList<Worker>(list); 
    workerBindingSource.DataSource = sortableList; 
} 

一切名爲 「txt」 的前綴是TextBox以及帶「cbo」前綴的所有內容均爲ComboBoxComboBoxes包含用戶可以選擇的某些枚舉。如果ComboBox顯示枚舉值「Empty」這意味着用戶沒有選擇任何內容(與字符串的零長度值相同)。 SortableBindingList僅用於列標題點擊的排序目的,在這種情況下不重要。 ReadWorker方法只有在匹配所有搜索值時才返回工作人員,然後將其添加到列表中,然後將其顯示在DataGridView之後。 FillList方法將數據庫的工作人員的整個列表設置爲DataBindingSource


ReadWorker方法:

public static Worker ReadWorker(int workerID) 
{ 
    var worker = ReadEverything().Where(x => x.ID == workerID).FirstOrDefault(); 

    return worker; 
} 

方法ReadEverything從數據庫返回所有工人的名單。

我正在尋找一種LINQ解決方案(或任何其他更好的解決方案)以大幅度降低我的代碼並使其更具可讀性,但是由於我對編程和LINQ仍然很新穎,我無法自己弄清楚。所以我想知道你們是否可以幫助我,或者至少讓我指向正確的方向。

在此先感謝!

+0

'ReadEverything'是否將數據庫命名爲'Select * from table'?爲什麼不使用'select * from table where id = workerid'? –

+0

*'workerBindingSource'的*類型是什麼?它實際上是一個'System.Windows.Forms.BindingSource'嗎? –

+0

如果它是綁定源,你可以使用'BindingSoure.Filter'屬性過濾列表 –

回答

2

如果你的datagridview已經擁有來自數據庫的所有數據,我建議你不要再去數據庫搜索,因爲你已經擁有了所有的數據。只需在內存中過濾並顯示它。

如果因爲某些原因需要訪問數據庫,沒有理由從數據庫獲取所有數據並在客戶端過濾它,只需將「workerId」傳遞給存儲過程,讓SP添加到適當的位置條件並返回過濾的數據。

要使用綁定源在內存中過濾器:

bindingSource.Filter = "columnname = 'value'"; 

private void button1_Click(object sender, EventArgs e) 
{ 
    string searchValue = textBox1.Text; 

    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; 
    bindingSource.Filter = string.Format("{0} = '{1}'","YourColumnName", searchValue); 
    //here you can do selection if you need 
} 

To remove filter use the following 

bindingSource.RemoveFilter(); 
//or 
bindingSource.Filter = null; 

最初發布的答案here

+0

是的我已經在發佈我的問題之前檢查了原始答案,但我試圖這樣做。如果可能的話,我只想用linq優化代碼。 =( – msmolcic

+0

你必須瞭解一件事情,Linq沒有優化任何東西,它使得代碼易讀易懂,就是這樣,Neverthless「我愛linq」但是,在這種情況下,我建議你使用'bindingSource.Filter'只是爲了這個目的而設計的,你需要生成查詢來過濾數據,就是這樣 –

+1

好的,謝謝你,我重視更有經驗的程序員的解釋,我提出了你的答案,並試圖這樣做,如果沒有出現,我會等待另一個答案和意見一段時間,我會接受你的答案。乾杯! – msmolcic