2017-08-07 10 views
0

DataGridにSQLクエリを設定しようとしていて、そのデータグリッドをフィルタできるようにしようとしています。これまでのところ私はこれを持っている:指定DataGridにバインディングが設定されていません

XAML

<Window x:Name="ResultsWindow" x:Class="PixsellSheet.PixsellOrders" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:PixsellSheet" 
    mc:Ignorable="d" Height="721.175" Width="1549.21" Title="edit" WindowStartupLocation="CenterScreen"> 
<Grid> 
    <DataGrid x:Name="Grid" HorizontalAlignment="Stretch" Height="Auto" Margin="20,55,20,40" VerticalAlignment="Stretch" Width="Auto" ItemsSource="{Binding DataGridColletion}"> 
     <DataGrid.Resources> 
      <ContextMenu x:Key="DataGridColumnHeaderContextMenu"> 
       <MenuItem Header="Filter" Click="MenuItem_Click"/> 
      </ContextMenu> 
      <Style TargetType="{x:Type DataGridColumnHeader}"> 
       <Setter Property="ContextMenu" Value="{StaticResource DataGridColumnHeaderContextMenu}"/> 
      </Style> 
     </DataGrid.Resources> 
    </DataGrid> 
    <Button x:Name="BtnBack" HorizontalAlignment="Left" Margin="20,10,0,0" VerticalAlignment="Top" Width="30" Click="BtnBack_Click" Padding="20,0,5,0"> 
     <Button.Template> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Grid> 
        <Image Name="Normal" Source="back.png"/> 
        <Image Name="Pressed" Source="back_pressed.png"/> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsPressed" Value="True"> 
         <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/> 
         <Setter TargetName="Pressed" Property="Visibility" Value="Visible"/> 
        </Trigger> 
        <Trigger Property="IsPressed" Value="False"> 
         <Setter TargetName="Normal" Property="Visibility" Value="Visible"/> 
         <Setter TargetName="Pressed" Property="Visibility" Value="Hidden"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Button.Template> 
    </Button> 
    <TextBox Height="27" Margin="0,10,20,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Right" Width="524" Name="FilterBox" Padding="5,0,20,0"/> 
    <Label Content="Filter:" HorizontalAlignment="Right" Margin="0,10,550,0" VerticalAlignment="Top" Padding="5,0,5,0"/> 
    <Grid HorizontalAlignment="Left" Height="25" Margin="60,10,0,0" VerticalAlignment="Top" Width="20" /> 
</Grid> 

C#

public partial class PixsellOrders : Window, INotifyPropertyChanged 
{ 
    public ICollectionView _dataGridCollection; 
    private string _filterString; 

    public ICollectionView DataGridCollection 
    { 
     get { return _dataGridCollection; } 
     set { _dataGridCollection = value; NotifyPropertyChanged("DataGridCollection"); } 
    } 

    public PixsellOrders(string windowTitle) 
    { 
     InitializeComponent(); 

     string query = ""; 

     ResultsWindow.Title = windowTitle; 

     Console.WriteLine(windowTitle); 

     if (windowTitle == "PixSell Orders All") 
     { 
      query = "EXEC Reporting.dbo.Pixsell_Orders_All"; 
     } 
     else if (windowTitle == "PixSell Orders Eday") 
     { 
      query = "EXEC Reporting.dbo.Pixsell_Orders_Eday"; 
     } 

     Console.WriteLine(query); 

     try 
     { 
      DataTable pixsellOrders = SqlConnect(query); 

      foreach (DataColumn column in pixsellOrders.Columns) 
      { 
       column.ReadOnly = true; 

       if (column.ColumnName == "Person" && windowTitle != "PixSell Orders All") 
       { 
        pixsellOrders.Columns["Person"].ReadOnly = false; 
       } 
       else if (column.ColumnName == "Sales Notes" && windowTitle != "PixSell Orders All") 
       { 
        pixsellOrders.Columns["Sales Notes"].ReadOnly = false; 
       } 
      } 

      DataGridCollection = CollectionViewSource.GetDefaultView(pixsellOrders.AsEnumerable()); 
      DataGridCollection.Filter = new Predicate<object>(Filter); 

      pixsellOrders.ColumnChanged += new DataColumnChangeEventHandler(Column_Changed); 

     } 
     catch (SqlException sqlEr) 
     { 
      Console.WriteLine(sqlEr); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(string property) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
      _dataGridCollection.Refresh(); 
     } 
    } 

    public DataTable SqlConnect(string query) 
    { 
     SqlConnection ohsql1; 
     string sqlQuery = query; 

     ohsql1 = new SqlConnection("Data Source=OHSQL1;Initial Catalog=Reporting;Integrated Security = true"); 


     DataTable table = new DataTable(); 

     try 
     { 
      //connect 
      ohsql1.Open(); 

      //fill datatable with results 
      SqlDataAdapter a = new SqlDataAdapter(sqlQuery, ohsql1); 

      //fill table 
      a.Fill(table); 

      //kill connection 
      a.Dispose(); 
      ohsql1.Close(); 

     } 
     catch (SqlException e) 
     { 
      Console.WriteLine("SQL ERROR: " + e); 
     } 

     return table; 
    } 

    private void MenuItem_Click(object sender, RoutedEventArgs e) 
    { 
     _filterString = FilterBox.Text; 

     if (_filterString == "") 
     { 
      Console.WriteLine("no filter"); 
      return; 
     } 
     else 
     { 
      Console.WriteLine(_filterString); 
      FilterCollection(); 
     } 
    } 

    private void FilterCollection() 
    { 
     if (_dataGridCollection != null) 
     { 
      _dataGridCollection.Refresh(); 
     } 
    } 

    private bool Filter(object obj) 
    { 
     if (obj is DataRow data) 
     { 
      if (!string.IsNullOrEmpty(_filterString)) 
      { 
       return data["CUNAME"].ToString().Contains(_filterString); 
      } 
      else 
      { 
       return true; 
      } 

     } 
     return false; 
    } 

"CUNAME" 欄は、単なるテストで、最終的に私はそれがどの列を知っていたいですフィルターボタンが押されました。

私が得ている問題は、DataGridが空に戻ることです。私はGrid.ItemsSource = pixsellOrders.DefaultView(またはその効果に何か、正確な構文を覚えていない)を行うとき、それはうまく動作し、グリッドに値を設定します。

私は間違いなくIEnumerableになるリストに変更しようとしましたが、データグリッドにもデータが入力されていませんでした。 AutoGenerateColumns(trueまたはfalseのいずれか)を追加しても効果はありません。出力にエラーは表示されません。すべてのフィルタ部分をコメントアウトすることも効果がありません。コンテキストメニューを削除しても効果はありません。 AsEnumerable()を削除してもグリッドの集計には影響ありませんが、DataGridCollection.Filterではスローしてエラーが発生します。

これはどこが間違っているのでしょうか?そして、事前に

+0

よくキャッチ!しかし、残念ながら問題ではありません(私が試した後にバインディングを追加したときに私が間違っていた) – Jackimedes

答えて

1

ありがとう

)あなたは、私たちが同様に本当に便利であること、私がチェック(というよりも、すべての列をハードコーディング)をカラム名を取得する方法について行くかもしれない方法をアドバイスすることができれば WindowDataContextを設定します。自分自身:

public PixsellOrders(string windowTitle) 
{ 
    InitializeComponent(); 
    DataContext = this; 

    //... 
} 
+0

これは間違いなく私のグリッドに追加されます!しかし、AsEnumerableはグリッドにナンセンスを渡しますが、それを削除すると、指定されたメソッドはサポートされません。 DataGridCollection.Filter = new Predicate (Filter); – Jackimedes

+0

残念ながら同じエラー – Jackimedes

+0

新しい問題が発生した場合は、新しい質問をしてください。元の質問はバインディングと空のDataGridに関するものでした。 – mm8

関連する問題