WPF Foreach行可见性集

时间:2016-05-12 09:21:04

标签: c# wpf datagridview foreach filter

我正在使用WPF创建一个应用程序,其中搜索字符串将过滤数据网格视图,但它可以包含多个搜索字符串。 我尝试在下面创建一个过滤器功能,但它在我设置可见性的最后一点并没有完全正常工作。你能帮我理解我哪里出错了吗? 感谢

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

1 个答案:

答案 0 :(得分:0)

只有在合并后的字符串Visible中存在来自value的{​​{1}}的{​​{1}}时,才会将行的可见性设置为array。如果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