数据库
首页 > 数据库> > 首页> C#>如何做到既过滤和分组列表视图从SQL

首页> C#>如何做到既过滤和分组列表视图从SQL

作者:互联网

我如何让我的Listview进行分组和过滤,因为我只能一次执行一件事情.我几乎已经尝试了所有可能的方法,但是没有一个起作用.当我删除

 public string SelectedParam { get { return _selectedParam; } set { _selectedParam = value; OnPropertyChanged("SelectedParam");
        if (_selectedParam == "Krydsmål") { BindData(); } else { hjuldata.ItemsSource = FilterKategori().Tables[0].DefaultView; ; } } }

则分组有效,但过滤不起作用

我想知道是否可以使用sql进行填充,而不是同时进行填充和过滤,然后让listview像您手动添加项目一样进行过滤

我的组合框用于过滤:

 <ComboBox x:Name="Krydsmålbox" Foreground="#FFEAEAEA" Background="#FF303030" FontSize="12" 
Style="{StaticResource ComboBoxTest2}"  ItemTemplate="{StaticResource cmbTemplate2}" 
ItemsSource="{Binding}"  SelectedValuePath="Krydsmålene"
SelectedValue = "{Binding SelectedParam, RelativeSource={RelativeSource FindAncestor, 
                  AncestorType={x:Type Window}},UpdateSourceTrigger=PropertyChanged}" BorderBrush="#FF303030" Height="40" DockPanel.Dock="Top" Margin="586,42,379,0"/>

列表显示

 <ListView x:Name="hjuldata" BorderBrush="#FF303030" Foreground="#FF00FB0B"  ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" Background="#FF303030" ItemsSource="{Binding}" Margin="-160,242,11,22" Grid.ColumnSpan="6" Grid.Row="3" Style="{DynamicResource ListViewStyle2}" DockPanel.Dock="Bottom" Height="576">
 <ListView.View>
 <GridView>
 <GridView.ColumnHeaderContainerStyle>
 <Style TargetType="{x:Type GridViewColumnHeader}">
 <Setter Property="Background" Value="Black" />
 <Setter Property="Foreground" Value="#FFEAEAEA"/>
 <Setter Property="FontWeight" Value="Bold" />
 </Style>
 </GridView.ColumnHeaderContainerStyle>
 <GridViewColumn Header="" >
 <GridViewColumn.CellTemplate>
 <DataTemplate>
 <Image Source="{Binding Billed, Converter={StaticResource nullImageConverter}}" Width="20" Height="20" Stretch="Fill" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,15,0"/>
 </DataTemplate>
 </GridViewColumn.CellTemplate>
 </GridViewColumn>
 <GridViewColumn Header="Model" Width="140" >
 <GridViewColumn.CellTemplate>
 <DataTemplate>
 <TextBlock x:Name="Txt" Text="{Binding Model}" Foreground="#FF00FB0B" />
 </DataTemplate>
 </GridViewColumn.CellTemplate>
 </GridViewColumn>
 <GridViewColumn Header="Årgang" Width="100">
 <GridViewColumn.CellTemplate>
 <DataTemplate>
 <TextBlock x:Name="Txt" Text="{Binding Årgang}" Foreground="#FF00FB0B" />
 </DataTemplate>
 </GridViewColumn.CellTemplate>
 </GridViewColumn>
 <GridViewColumn Header="Motor Type" Width="150" >
 <GridViewColumn.CellTemplate>
 <DataTemplate>
 <TextBlock x:Name="Txt" Text="{Binding [Motor Type]}" Foreground="#FF00FB0B" />
 </DataTemplate>
 </GridViewColumn.CellTemplate>
 </GridViewColumn>
 <GridViewColumn>
  <GridViewColumn.CellTemplate>
  <DataTemplate>
  <TextBlock x:Name="Txt" Text="{Binding Krydsmålet}" Foreground="#FF00FB0B" />
  </DataTemplate>
  </GridViewColumn.CellTemplate>
  </GridViewColumn>
  <GridViewColumn>
  <GridViewColumn.CellTemplate>
  <DataTemplate>
  <TextBlock x:Name="Txt" Text="{Binding Centerhul}" Foreground="#FF00FB0B" />
  </DataTemplate>
  </GridViewColumn.CellTemplate>
  </GridViewColumn>
  <GridViewColumn Header="ET" Width="auto">
  <GridViewColumn.CellTemplate>
  <DataTemplate>
  <TextBlock x:Name="Txt" Text="{Binding ET}" Foreground="#FF00FB0B" />
  </DataTemplate>
  </GridViewColumn.CellTemplate>
  </GridViewColumn>
  <GridViewColumn Header="Bolter" Width="100">
  <GridViewColumn.CellTemplate>
  <DataTemplate>
  <TextBlock x:Name="Txt" Text="{Binding Bolter}" Foreground="#FF00FB0B" />
  </DataTemplate>
  </GridViewColumn.CellTemplate>
  </GridViewColumn>
  <GridViewColumn Header="Dæk" Width="300">
  <GridViewColumn.CellTemplate>
  <DataTemplate>
  <TextBlock x:Name="Txt" Text="{Binding Dæk}" Foreground="#FF00FB0B" />
  </DataTemplate>
  </GridViewColumn.CellTemplate>
  </GridViewColumn>
  <GridViewColumn Header="Fælge" Width="200">
  <GridViewColumn.CellTemplate>
  <DataTemplate>
  <TextBlock x:Name="Txt" Text="{Binding Fælge}" Foreground="#FF00FB0B" />
  </DataTemplate>
  </GridViewColumn.CellTemplate>
  </GridViewColumn>
  </GridView>
  </ListView.View>

分组方式

  <ListView.GroupStyle>
  <GroupStyle>
  <GroupStyle.ContainerStyle>
  <Style TargetType="{x:Type GroupItem}">
  <Setter Property="Template">
  <Setter.Value>
  <ControlTemplate>
  <Expander IsExpanded="False"  BorderBrush="#FFEAEAEA" BorderThickness="0,0,0,1" >
 <Expander.Header>
 <StackPanel Orientation="Horizontal" DataContext="{Binding Items}">
 <Image Source="{Binding Billed, Converter={StaticResource nullImageConverter}}" Width="20" Height="20" Stretch="Fill" VerticalAlignment="Center"  Margin="0,0,15,0"/>
 <TextBlock  Text="{Binding Mærke}" FontWeight="Bold" Foreground="#FFEAEAEA" FontSize="22" VerticalAlignment="Bottom" />
 <TextBlock Text="{Binding Krydsmålene}"  FontWeight="Bold" Foreground="#FFFBFB00" FontSize="22" VerticalAlignment="Bottom" Margin="0,0,150,0" TextAlignment="Center" />
 </StackPanel>
 </Expander.Header>
 <ItemsPresenter />
 </Expander>
 </ControlTemplate>
 </Setter.Value>
 </Setter>
 </Style>
 </GroupStyle.ContainerStyle>
 </GroupStyle>
 </ListView.GroupStyle>
 </ListView>

CS:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _selectedParam;
    public MainWindow()
    {
        InitializeComponent();
        BindData();
        ICollectionView dataView = CollectionViewSource.GetDefaultView(hjuldata.ItemsSource);
        dataView.GroupDescriptions.Add(new PropertyGroupDescription("Mærke"));

    }

     public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public string SelectedParam { get { return _selectedParam; } set { _selectedParam = value; OnPropertyChanged("SelectedParam");
    if (_selectedParam == "Krydsmål") { BindData(); } else { hjuldata.ItemsSource = FilterKategori().Tables[0].DefaultView; ; } } }

    private void BindData()
    {
        hjuldata.ItemsSource = Kategori().Tables[0].DefaultView;
    }
    public DataSet Kategori()
    {

        Data = @"Select ps.Mærket AS Mærke, P.DataID, P.Billed, P.Model, P.Årgang, P.[Motor Type], P.Krydsmålet, 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";
        //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 (cmd = new SqlCommand(Data, conn))
            {                
                SqlDataAdapter adapters = new SqlDataAdapter(cmd);

                //Fill the result set

                adapters.Fill(dsdata);
                conn.Close();
            }
        }
        return dsdata;
    }

    public DataSet FilterKategori()
    {
        Data = @"Select ps.Mærket AS Mærke, P.DataID, P.Billed, P.Model, P.Årgang, P.[Motor Type], P.Krydsmålet, 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ålet = @param1";

        //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 (cmd = new SqlCommand(Data, conn))
            {
                cmd.Parameters.AddWithValue("@param1", SelectedParam);
                SqlDataAdapter adapters = new SqlDataAdapter(cmd);
                //Fill the result set
                adapters.Fill(dsdata);
                conn.Close();
            }
        }  
        return dsdata;
    }

有人可以帮我吗?

解决方法:

我认为这是你应该读的东西
How to: Group, Sort, and Filter Data in the DataGrid Control

我已经测试过了,它可以与您的数据结构一起使用,您不必对过滤进行任何操作,只需更改分组部分和colectionview

标签:grouping,wpf,c,sql-server,filtering
来源: https://codeday.me/bug/20191118/2029853.html