2016-12-23 85 views
0

我有一个dataGrid在我的app.I使用TextChanged事件来过滤数据grid.I有四个文本框用于过滤。我正在使用SQL查询与LIKE 。 当我只用两个文本框使用过滤器时,此方法工作正常。但如果我使用三个(四个)(因为我需要四个),它不会过滤数据网格right.It不显示所有匹配,并且当我清除文本框时,它不会重置dataGrid以显示整个表格。dataGrid搜索与SQL LIKE查询不工作,因为它应该

我的XAML:

<DataGrid x:Name="dataGridSearch" HorizontalAlignment="Center" VerticalAlignment="Top" Height="227" Width="990" Margin="0,202,0,0" ItemsSource="{Binding DataSource}" AutoGenerateColumns="False" IsReadOnly="True"> 
        <DataGrid.Columns> 
         <DataGridTextColumn Header="Name" Binding="{Binding name}" Width="200" /> 
         <DataGridTextColumn Header="Type" Binding="{Binding type}" Width="300"/> 
         <DataGridTextColumn Header="City" Binding="{Binding city}" Width="250" /> 

         <DataGridTextColumn Header="Place" Binding="{Binding place}" Width="*" /> 


        </DataGrid.Columns> 
       </DataGrid> 

我对TextChanged事件代码:

using (SqlConnection sc = new SqlConnection(ConString)) 
     { 
      sc.Open(); 
      string query_search = "SELECT * FROM object WHERE name LIKE @name AND type LIKE @type AND city LIKE @city AND place LIKE @place"; 
      SqlCommand com = new SqlCommand(query_search, sc); 
      com.Parameters.AddWithValue("@name", "%" + textBoxPlace.Text + "%"); 
      com.Parameters.AddWithValue("@type", "%" + textBoxType.Text + "%"); 
      com.Parameters.AddWithValue("@city", "%" + textBoxCity.Text + "%"); 
      com.Parameters.AddWithValue("@place", "%" + textBoxPlace.Text + "%"); 


      using (SqlDataAdapter adapter = new SqlDataAdapter(com)) 
      { 
       DataTable dt = new DataTable(); 
       adapter.Fill(dt); 
       dataGridSearch.ItemsSource = dt.DefaultView; 
      } 
      sc.Close(); 
     } 

此外,在窗的打开,我有在开始填充DataGrid中的一个方法

public void fillGrid() 
    { 

     using (SqlConnection sc = new SqlConnection(ConString)) 
     { 
      sc.Open(); 
      string query = "SELECT * FROM object"; 
      SqlCommand com = new SqlCommand(query, sc); 

      using (SqlDataAdapter adapter = new SqlDataAdapter(com)) 
      { 
       DataTable dt = new DataTable(); 
       adapter.Fill(dt); 
       adapter.Update(dt); 
       // dataGridSvi.AutoGenerateColumns = false; 

       dataGridSearch.ItemsSource = dt.DefaultView; 
      } 
      sc.Close(); 
     } 
    } 

回答

1
com.Parameters.AddWithValue("@name", "%" + textBoxPlace.Text + "%"); 
com.Parameters.AddWithValue("@type", "%" + textBoxType.Text + "%"); 
com.Parameters.AddWithValue("@city", "%" + textBoxCity.Text + "%"); 
com.Parameters.AddWithValue("@place", "%" + textBoxType.Text + "%"); 

检查文本框,你叫相同的文本框两次。也许这就是问题所在

哦以及有关重置您的网格,你可以叫fillGrid()一个更多的时间,当所有的文本框将明确

+0

这仅仅是为stackoverfow question.I错字都有了正确的进入在我的code.sorry的typo.also它看起来像来自数据库的一些领域,它工作正常。但对于一些它不 – Alexander

+0

好吧,如果你用'按钮'做它?不是'textchange事件'?你能发表确切的代码吗? – Noah