2016-05-12 72 views
0

我正在使用WPF創建一個應用程序,其中一個搜索字符串將過濾一個datagridview,但它可以包含多個搜索字符串。 我試着在下面創建一個過濾器函數,但是它在最後一個位置設置可見性的位置並不完全正常。你們能幫我瞭解我哪裏出了問題嗎? 感謝WPF Foreach Row Visibility Set

private void BindGrid(string parameter) 
    { 
     string[] array = parameter.Split(); 
     string constring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\SearchTable.mdf;Integrated Security=True;Connect Timeout=30"; 
     using (SqlConnection con = new SqlConnection(constring)) 
     { 
      con.Open(); 
      using (SqlCommand cmd = new SqlCommand("SELECT * FROM Projects", con)) 
      { 
       cmd.CommandType = CommandType.Text; 
       using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) 
       { 
        using (DataTable dt = new DataTable()) 
        { 
         sda.Fill(dt); 
         dataGridView1.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = dt }); 

         foreach (DataRow row in dt.Rows) 
         { 
          SqlDataReader reader = cmd.ExecuteReader(); 
          if (reader.HasRows) 
          { 
           while (reader.Read()) 
           { 
            int index = reader.GetInt32(0); 
            string a = reader.GetString(1); 
            string b = reader.GetString(2); 
            string c = reader.GetString(3); 
            string d = reader.GetString(4); 
            string e = reader.GetString(5); 
            string f = reader.GetString(6); 
            string g = reader.GetString(7); 
            string h = reader.GetString(8); 
            string i = reader.GetString(9); 
            string j = reader.GetString(10); 
            string t = a + " " + b + " " + c + " " + d + " " + e + " " + f + " " + g + " " + h + " " + i + " " + j; 

            foreach (string value in array) 
            { 
             if (t.IndexOf(value, StringComparison.CurrentCulture) == -1) 
             { 
              row.Visibility = Visibility.Collapsed; 
             } 
             else if (t.IndexOf(value, StringComparison.CurrentCulture) > 0) 
             { 
              row.Visibility = Visibility.Visible; 
             } 
            } 
           } 
          } 
          reader.Close(); 
         }       
         rows = dataGridView1.Items.Count.ToString();   
         Rows.Content = rows + " Entries"; 
        } 
       } 
      } 
      con.Close(); 
     } 
    } 
+0

WPF不支持'DataGridView'構造。 (另外'DataGridView'沒有'Items'屬性。)也許你的意思是['DataGrid'](https://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid(v = vs。 110)的.aspx)?如果是這樣,請編輯您的問題標籤,以便您的問題可以從正確的人獲得更多關注。 – OhBeWise

回答

0

你行的能見度只有最後valuearray設置爲Visible存在合併字符串t英寸此外該行沒有被設置爲可見,如果value是在字符串的開頭(使IndexOf回報0

foreach (string value in array) 
{ 
    // each time we are here, we are going to overwrite the row.Visibility, 
    // so only the value that was set in the last iteration stays. 
    if (t.IndexOf(value, StringComparison.CurrentCulture) == -1) 
    { 
     row.Visibility = Visibility.Collapsed; 
    } 
    // also > 0 restricts the substring to be found in the beginning of the string 
    // you've probably meant >= 0 or != -1 
    else if (t.IndexOf(value, StringComparison.CurrentCulture) > 0) 
    { 
     row.Visibility = Visibility.Visible; 
    } 
} 

我想你想將它設置爲可見,如果至少一個值的一個子合併字符串t

row.Visibility = array.Any(value => t.IndexOf(value, StringComparison.CurrentCulture) != -1); 

第二個問題是,該環與該DataTable行,不DataGrid行操作。在更新BindGrid方法中的網格時,可以將網格僅綁定到要顯示的行。

private void BindGrid(string parameter) 
{ 
    string[] array = parameter.Split(); 
    string constring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\SearchTable.mdf;Integrated Security=True;Connect Timeout=30"; 
    using (SqlConnection con = new SqlConnection(constring)) 
    { 
     con.Open(); 
     using (SqlCommand cmd = new SqlCommand("SELECT * FROM Projects", con)) 
     { 
      cmd.CommandType = CommandType.Text; 
      using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) 
      { 
       using (DataTable dt = new DataTable()) 
       { 
        sda.Fill(dt); 
        List<DataRow> rowsToShow = new List<DataRow>(); 
        foreach (DataRow row in dt.Rows) 
        { 
         SqlDataReader reader = cmd.ExecuteReader(); 
         if (reader.HasRows) 
         { 
          while (reader.Read()) 
          { 
           int index = reader.GetInt32(0); 
           string a = reader.GetString(1); 
           string b = reader.GetString(2); 
           string c = reader.GetString(3); 
           string d = reader.GetString(4); 
           string e = reader.GetString(5); 
           string f = reader.GetString(6); 
           string g = reader.GetString(7); 
           string h = reader.GetString(8); 
           string i = reader.GetString(9); 
           string j = reader.GetString(10); 
           string t = a + " " + b + " " + c + " " + d + " " + e + " " + f + " " + g + " " + h + " " + i + " " + j; 

           if (array.Any(value => t.IndexOf(value, StringComparison.CurrentCulture) != -1)) 
           { 
            rowsToShow.Add(row); 
           } 
          } 
         } 
         reader.Close(); 
        } 
        dataGridView1.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = dt }); 
        rows = dataGridView1.Items.Count.ToString(); 
        Rows.Content = rows + " Entries"; 
       } 
      } 
     } 
     con.Close(); 
    } 
} 
+0

謝謝filhit,但是我收到「Visibility」這個詞的錯誤 - 'System.Data.DataRow'沒有包含'Visibility'的定義,也沒有接受'System'類型的第一個參數的擴展方法'Visibility'。 Data.DataRow'可以找到(你是否缺少使用指令或程序集引用? – Dinoduck94

+0

@ Dinoduck94哦...現在我看到了這個,你正在迭代'DataTable'中的行,而不是'DataGrid'行。你可以嘗試從我的答案更新的代碼 – filhit

+0

非常感謝您的幫助。我使用上面的代碼,但我把所需行的索引放入一個字符串數組,並通過'RowFilter'使用字符串數組過濾行。再次 – Dinoduck94