2016-05-12 7 views
0

WPFを使用してアプリケーションを作成していますが、検索文字列でデータグリッドビューをフィルタリングしますが、複数の検索文字列を含めることができます。 私は下のフィルタ関数を作成しようとしましたが、可視性を設定した最後のビットではうまくいきません。あなたは私が間違っているところを理解するのを手伝ってもらえますか? おかげWPF Foreach行の可視性セット

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

あなたはarrayから最後valueを組み合わせた文字列tに存在する場合にのみVisibleに行の可視性を設定しています。 valueは、文字列の先頭にある場合、私はあなたが少なくとも1つの値がの部分文字列である場合には目に見えるに設定したいと思います

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; 
    } 
} 

IndexOf戻り0ように)また、行は、可視に設定されていません結合された文字列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'の定義がなく、 'Visibility'のタイプが 'System'の最初の引数を受け入れる拡張メソッドはありません。 – Dinoduck94

+0

@ Dinoduck94 oh ...これを見ると、DataGridの行ではなく、 'DataTable'の行を反復処理しています。 – filhit

+0

私は上記のコードを使用しましたが、必要な行のインデックスを文字列配列に入れ、 'RowFilter'を介して文字列配列を使用して行をフィルタリングしました。再び – Dinoduck94