looking for some solutions? You are welcome.

SOLVED: When listbox item is selected display related data in another listbox

Java2YourScript:

I am building an app with WPF C# and trying to display values in a list box which shows when an item from another list box is selected. Both list boxes need to get data from my SQL database.

listboxes when nothing is selected:

How listboxes should look when nothing is selected

kistboxes when an item is selected:

How listboxes should look when an item is selected

This is how I display the values in the first list box (before anyone says it, I know it is vulnerable to SQL injection).

C#:

public void TradeList() {
    DataTable dt = new DataTable();
    SqlDataAdapter adpt = new SqlDataAdapter("SELECT DISTINCT Trade from tblTrades", sqlConTwo);
    adpt.Fill(dt);

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

XAML:

<StackPanel>
  <TextBox x:Name="TradesSelected" Width="665" BorderBrush="#FF939393" Padding="2" BorderThickness="1"></TextBox>
  <ListBox SelectionMode="Multiple" x:Name="Trade" Width="665" Height="100" BorderBrush="#FF939393" Padding="2" BorderThickness="1 0 1 1" ItemsSource="{Binding Path=Trade}" SelectionChanged="Trade_SelectionChanged" >
    <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
        <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
      </Style>
    </ListBox.ItemContainerStyle>
  </ListBox>
</StackPanel>
<StackPanel Margin="10 0 0 0">
  <TextBox x:Name="SkillSelected" Width="665" BorderBrush="#FF939393" Padding="2" BorderThickness="1"></TextBox>
  <ListBox SelectionMode="Multiple" x:Name="Skills" Width="665" Height="100" BorderBrush="#FF939393" Padding="2" BorderThickness="1 0 1 1" ItemsSource="{Binding Path=Skills}">
    <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
        <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
      </Style>
    </ListBox.ItemContainerStyle>
  </ListBox>
</StackPanel>

In my table, the column with the skills in it is called Skills. I have found a way to do it by hard-coding all the trades and skills in, but this is not ideal as there are a lot of rows in my table, and this would be very time-consuming.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent