简体   繁体   中英

Show data in list box based on multiple selected list box items

I have hit a wall with my project I am working on built in WPF C#

I am trying to get a list box that allows multiple items to be selected, to then show related data in another list box.

I have managed to get it to work for the first item that was selected in the list box, but it does not show the other selected items data.

Each selected item will show in a text box and separated by comma.

选择之前

选择之后

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>

Any help would be appreciated, thanks.

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

This query will give you right result. But,

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

This query will give you an empty result. You need to use it like this,

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

You can use this SQL Query in your code:

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

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM