根据多个选定的列表框项目在列表框中显示数据

时间:2019-03-08 13:39:21

标签: c# sql-server wpf

我正在使用WPF C#构建的项目碰壁

我试图得到一个允许选择多个项目的列表框,然后在另一个列表框中显示相关数据。

我设法使它适用于列表框中选择的第一个项目,但它不显示其他选定项目数据。

每个选定的项目将显示在文本框中,并以逗号分隔。

Before Selection

After Selection

C#:

public void AreaList()
    {

        DataTable dt = new DataTable();
        SqlDataAdapter adpt = new SqlDataAdapter("SELECT DISTINCT Town from tblAllPostCodes", sqlConTwo);
        adpt.Fill(dt);

        foreach(DataRow dr in dt.Rows)
        {
            Area.Items.Add(dr["Town"].ToString());
        }

    }

private void Area_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        AreasSelected.Text = "";
        foreach (object area in Area.SelectedItems)
        {
            AreasSelected.Text += (AreasSelected.Text == "" ? "" : " , ") + area.ToString();
        }


            DataTable dt = new DataTable();
            SqlDataAdapter adpt = new SqlDataAdapter("SELECT PostCode,Town FROM tblAllPostCodes where Town='" + AreasSelected.Text + "'", sqlConTwo);
            adpt.Fill(dt);

            foreach (DataRow dr in dt.Rows)
            {
                AreaPostCode.Items.Add(dr["PostCode"].ToString());
            }

        }

XAML:

 <WrapPanel Margin="0,20,0,0">
                <StackPanel>

                    <TextBox materialDesign:HintAssist.Hint="Areas" x:Name="AreasSelected" Width="665" BorderBrush="#FF939393" Padding="2" BorderThickness="1"></TextBox>
                    <ListBox SelectionMode="Multiple" x:Name="Area" Width="665" Height="100" BorderBrush="#FF939393" Padding="2" BorderThickness="1 0 1 1" ItemsSource="{Binding Path=Area}" SelectionChanged="Area_SelectionChanged" >
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
                            </Style>
                        </ListBox.ItemContainerStyle>
                    </ListBox>
                </StackPanel>
                <StackPanel Margin="10 0 0 0">
                    <TextBox materialDesign:HintAssist.Hint="Area Post Codes" x:Name="PostCodesSelected" Width="665" BorderBrush="#FF939393" Padding="2" BorderThickness="1"></TextBox>
                    <ListBox SelectionMode="Multiple" x:Name="AreaPostCode" Width="665" Height="100" BorderBrush="#FF939393" Padding="2" BorderThickness="1 0 1 1" ItemsSource="{Binding Path=AreaPostCode}">
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
                            </Style>
                        </ListBox.ItemContainerStyle>
                    </ListBox>
                </StackPanel>
            </WrapPanel>

谢谢您的任何帮助。

1 个答案:

答案 0 :(得分:3)

SELECT PostCode,Town FROM tblAllPostCodes where Town='ABERDOVEY'

此查询将为您提供正确的结果。但是,

SELECT PostCode,Town FROM tblAllPostCodes where Town='ABERDOVEY, ALDEBURGH, ...'

此查询将为您提供空结果。您需要像这样使用它,

SELECT PostCode,Town FROM tblAllPostCodes where Town in ('ABERDOVEY', 'ALDEBURGH', '...')

您可以在代码中使用此SQL查询:

private void Area_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        AreasSelected.Text = "";
        foreach (object area in Area.SelectedItems)
        {
            AreasSelected.Text += (AreasSelected.Text == "" ? "" : " , ") + area.ToString();
        }


            DataTable dt = new DataTable();
            SqlDataAdapter adpt = new SqlDataAdapter("SELECT PostCode,Town FROM tblAllPostCodes where Town in (" + string.Join(",", Area.SelectedItems.Cast<string>().Select(si => "'" + si.ToString() + "'").ToArray()) + ")", sqlConTwo);
            adpt.Fill(dt);

            foreach (DataRow dr in dt.Rows)
            {
                AreaPostCode.Items.Add(dr["PostCode"].ToString());
            }

        }