在组合框选择上从SQL Server过滤Listview

时间:2016-05-08 17:58:32

标签: c# sql-server wpf listview combobox

如何从组合框选择示例中过滤我的列表视图。

如果用户在组合框中选择了项5*108,则列表视图将显示5*108

中包含P.krydsmål项的所有项目

C#

     public partial class MainWindow : Window, INotifyPropertyChanged
{

    public SqlConnection conn;
    public SqlCommand cmd;
    string connStrings = ConfigurationManager.AppSettings["Sql"];
    string Data = @"Select   ps.Mærket AS Mærke, P.DataID, P.Billed, P.Model, P.Årgang, P.[Motor Type], P.Krydsmål, P.Centerhul, P.ET,P.Bolter, P.Dæk, P.Fælge ,PS.Krydsmålene from Data.Hjuldata P  inner join Data.Mærke PS on P.MærkeID = PS.MærkeID ORDER BY ps.Mærket";
    public event PropertyChangedEventHandler PropertyChanged;
    private string _selectedParam;
    public MainWindow()
    {
        InitializeComponent();
BindData()
ICollectionView dataView = CollectionViewSource.GetDefaultView(hjuldata.ItemsSource);
            dataView.GroupDescriptions.Add(new PropertyGroupDescription("Mærke"));

    }
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    public string SelectedParam { get { return _selectedParam; } set { _selectedParam = value; NotifyPropertyChanged("SelectedParam"); if (_selectedParam == "ingen") { BindData(); } else { hjuldata.ItemsSource = FilterKategori().Tables[0].DefaultView; ; } } }
    private void BindData()
    {
        hjuldata.ItemsSource = kategori().Tables[0].DefaultView;

    }
    public DataSet kategori()
    {
        //SQL statement to fetch entries from Hjuldata
        DataSet dsdata = new DataSet();

        //Open SQL Connection
        using (conn = new SqlConnection(connStrings))
        {
            conn.Open();

            //Initialize command object
            using (conn = new SqlConnection(connStrings))
            using (SqlCommand cmd = new SqlCommand(Data, conn))
            {                
                SqlDataAdapter adapters = new SqlDataAdapter(cmd);

                //Fill the result set
                adapters.Fill(dsdata);
                conn.Close();
            }
        }
        return dsdata;
    }
    public DataSet FilterKategori()
    {

    string Data = @"Select ps.Mærket AS Mærke, P.DataID, P.Billed, P.Model, 
P.Årgang, P.[Motor Type], P.Krydsmål, P.Centerhul, P.ET,P.Bolter, 
P.Dæk, P.Fælge ,PS.Krydsmålene from Data.Hjuldata P  
inner join Data.Mærke PS on P.MærkeID = PS.MærkeID 
WHERE Krydsmål = @param1";//only works with  '5*108'





//SQL statement to fetch entries from products
            DataSet dsdata = new DataSet();

            //Open SQL Connection
            using (conn = new SqlConnection(connStrings))
            {
                conn.Open();

                //Initialize command object
                using (SqlCommand cmds = new SqlCommand(Data, conn))
                {
                    cmds.Parameters.AddWithValue("@param1", SelectedParam);
                    SqlDataAdapter adapters = new SqlDataAdapter(cmds);
                    //Fill the result set
                    adapters.Fill(dsdata);
                }

            }
            return dsdata;
        }

Combobox

<ComboBox x:Name="comboBox_Copy" Width="150" Height="40" Foreground="#FF00FB0B" Background="#FF303030" FontSize="16"  Canvas.Left="1030" Canvas.Top="24" Style="{StaticResource ComboBoxTest2}"  SelectedItem = "{Binding SelectedParam, ElementName=win, UpdateSourceTrigger=PropertyChanged}">
                        <ComboBoxItem Content="Ingen"/>
                        <ComboBoxItem Content="3x98"/>
                        <ComboBoxItem Content="3x112"/>
                        <ComboBoxItem Content="4x98"/>
                        <ComboBoxItem Content="4x100"/>
                        <ComboBoxItem Content="4x108"/>
                        <ComboBoxItem Content="4x114.3"/>
                        <ComboBoxItem Content="4x160"/>
                        <ComboBoxItem Content="5x98"/>
                        <ComboBoxItem Content="5x100"/>
                        <ComboBoxItem Content="5x108"/>

2 个答案:

答案 0 :(得分:3)

我建议使用MVVM模式,让事情变得更轻松。但是当你使用背后的代码时:

1-在您的窗口上实现INotifyPropertyChanged:

public class MainWindow: Window, INotifyPropertyChanged
{
  //Existing code....


  public event PropertyChangedEventHandler PropertyChanged;

  private void NotifyPropertyChanged(String info)
  {
      if (PropertyChanged != null)
      {
          PropertyChanged(this, new PropertyChangedEventArgs(info));
      }
  }
}

2 - 定义表示组合框中所选项目的属性,并在其setter中,您可以触发过滤方法:

private string _selectedParam;
public string SelectedParam 
{
  get{return _selectedParam;}
  set
  {
     _selectedParam = value;  
     NotifyPropertyChanged("SelectedParam");
     hjuldata.ItemsSource = FilterKategori(_selectedParam).Tables[0].DefaultView;

  }
}

3 - 将ComboBox SelectedItem绑定到您的新属性:

<ComboBox x:Name="comboBox_Copy" 
          SelectedItem = "{Binding SelectedParam, ElementName=YourWindowsName, UpdateSourceTrigger=PropertyChanged}"
          Width="150" Height="40" 
          Foreground="#FF00FB0B" Background="#FF303030" FontSize="16"  
          Canvas.Left="1030" Canvas.Top="24">
     <ComboBox.Resources>
         <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="#FF303030" />
     </ComboBox.Resources>
     <ComboBoxItem Content="Ingen"/>
     <ComboBoxItem Content="3*98"/>
     <ComboBoxItem Content="3*112"/>
</ComboBox>

4 - 创建您的FilterKategori

public DataSet FilterKategori()
{

string Data = @"SELECT [DataID], [MærkeID], [Billed], [Model], [Årgang], [Motor Type], [Krydsmål], [Centerhul],
    [ET], [Møtrikker], [Bolter], [Dæk], [Fælge]
FROM 
    [OminiData].[Data].[Hjuldata]
WHERE
    Krydsmål = @param1";

//SQL statement to fetch entries from products
DataSet dsdata = new DataSet();

//Open SQL Connection
using (SqlConnection conns = new SqlConnection(connStrings))
{
conns.Open();

//Initialize command object
using (SqlCommand cmds = new SqlCommand(Data, conns))
{
   cmds.Parameters.AddWithValue("@param1", SelectedParam );
   SqlDataAdapter adapters = new SqlDataAdapter(cmds);
   //Fill the result set
  adapters.Fill(dsdata);
}

}    
return dsdata;
}

答案 1 :(得分:1)

现在你已经显示了SelectedParam的调试值,我遇到了问题。问题在于组合框。你的组合框应该是这样的:

<ComboBox x:Name="comboBox_Copy" Width="150" Height="40" Foreground="#FF00FB0B" Background="#FF303030" FontSize="16"  
       SelectedValuePath="Content"
       SelectedValue = "{Binding SelectedParam, ElementName=win, UpdateSourceTrigger=PropertyChanged}">
            <ComboBoxItem Content="Ingen"/>
            <ComboBoxItem Content="3x98"/>
            <ComboBoxItem Content="3x112"/>
            <ComboBoxItem Content="4x98"/>
            <ComboBoxItem Content="4x100"/>
            <ComboBoxItem Content="4x108"/>
            <ComboBoxItem Content="4x114.3"/>
            <ComboBoxItem Content="4x160"/>
            <ComboBoxItem Content="5x98"/>
            <ComboBoxItem Content="5x100"/>
            <ComboBoxItem Content="5x108"/>

</ComboBox>

注意SelectedValuePath。这将向SelectedParam发送字符串而不是对象ComboBoxItem。

在我的github中查看演示:DemoCombo

请注意名字,我的可以与你的不同......