2017-08-07 93 views
0

我试图用SQL查询填充DataGrid,然后能够过滤数据网格。到目前为止,我有这样的:未填充绑定的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谓词(Filter); – Jackimedes

+0

不幸的是有同样的错误 – Jackimedes

+0

如果您有新问题,请提出一个新问题。你原来的问题是关于绑定和空DataGrid。 – mm8