Skip to main content
 首页 » 编程设计

c#之在组合框选择上从 SQL Server 过滤 ListView

2024年02月05日12langtianya

如何从组合框选择示例中过滤掉我的 ListView 。

如果用户在组合框中选择项目 5*108,则 ListView 将显示其中包含 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 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"/> 

请您参考如下方法:

我建议使用 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 绑定(bind)到您的新属性:

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