Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Setting a listbox selected value when adding a new datagrid row

$
0
0

Hello,

I have a list box and a data grid. When I click on an item in the list box, the datagrid data shows according to the list box selected value.

Here is what I have:


Here is my code:

   public partial class MainWindow : Window
    {
        NorthwindEntities context = new NorthwindEntities();
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CollectionViewSource customerViewSource = ((CollectionViewSource)(this.FindResource("customerViewSource")));
            customerViewSource.Source = DataBase.data.getCustomerList();

            CollectionViewSource orderViewSource = ((CollectionViewSource)(this.FindResource("orderViewSource")));
            orderViewSource.Source = context.Orders.Where(o => o.CustomerID == CustomersListBox.SelectedValue).ToList();
        }

        private void CustomersListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var orders = context.Orders.Where(o => o.CustomerID == CustomersListBox.SelectedValue).ToList();
            CollectionViewSource orderViewSource = ((CollectionViewSource)(this.FindResource("orderViewSource")));
            orderViewSource.Source = orders;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "oh, oh!!!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }

Here is my XAML code:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:DataBase="clr-namespace:DataBase;assembly=DataBase" mc:Ignorable="d" x:Class="WpfMaserDetailDGToList2.MainWindow"
        Title="MainWindow" Height="485.961" Width="1159.038" Loaded="Window_Loaded"
        xmlns:staticData="clr-namespace:WpfMaserDetailDGToList2"><Window.Resources><CollectionViewSource x:Key="customerViewSource" d:DesignSource="{d:DesignInstance {x:Type DataBase:Customer}, CreateList=True}"/><CollectionViewSource x:Key="orderViewSource" d:DesignSource="{d:DesignInstance {x:Type DataBase:Order}, CreateList=True}"/></Window.Resources><Grid DataContext="{StaticResource customerViewSource}" ><ListBox x:Name="CustomersListBox" HorizontalAlignment="Left" Height="436" Margin="10,10,0,0" VerticalAlignment="Top" Width="147" DisplayMemberPath="CustomerID" ItemsSource="{Binding}" SelectedValuePath="CustomerID" SelectionChanged="CustomersListBox_SelectionChanged" SelectedIndex="0"/><DataGrid x:Name="orderDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding Source={StaticResource orderViewSource}}" Margin="162,10,177,10" RowDetailsVisibilityMode="VisibleWhenSelected" AlternatingRowBackground="AliceBlue"><DataGrid.Columns><DataGridTextColumn x:Name="customerIDColumn" Binding="{Binding CustomerID}" Header="Customer ID" Width="SizeToHeader" IsReadOnly="True" /><DataGridTextColumn x:Name="employeeIDColumn" Binding="{Binding EmployeeID}" Header="Employee ID" Width="SizeToHeader"/><DataGridTextColumn x:Name="freightColumn" Binding="{Binding Freight}" Header="Freight" Width="SizeToHeader"/><DataGridTemplateColumn x:Name="orderDateColumn" Header="Order Date" Width="120" ><DataGridTemplateColumn.CellTemplate><DataTemplate><DatePicker SelectedDate="{Binding OrderDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTextColumn x:Name="orderIDColumn" Binding="{Binding OrderID}" Header="Order ID" Width="SizeToHeader" Visibility="Hidden"/><DataGridTemplateColumn x:Name="requiredDateColumn" Header="Required Date" Width="120"><DataGridTemplateColumn.CellTemplate><DataTemplate><DatePicker SelectedDate="{Binding RequiredDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTextColumn x:Name="shipAddressColumn" Binding="{Binding ShipAddress}" Header="Ship Address" Width="SizeToHeader"/><DataGridTextColumn x:Name="shipCityColumn" Binding="{Binding ShipCity}" Header="Ship City" Width="SizeToHeader"/><DataGridTextColumn x:Name="shipCountryColumn" Binding="{Binding ShipCountry}" Header="Ship Country" Width="SizeToHeader"/><DataGridTextColumn x:Name="shipNameColumn" Binding="{Binding ShipName}" Header="Ship Name" Width="SizeToHeader"/><DataGridTemplateColumn x:Name="shippedDateColumn" Header="Shipped Date" Width="120"><DataGridTemplateColumn.CellTemplate><DataTemplate><DatePicker SelectedDate="{Binding ShippedDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTextColumn x:Name="shipPostalCodeColumn" Binding="{Binding ShipPostalCode}" Header="Ship Postal Code" Width="SizeToHeader"/><DataGridTextColumn x:Name="shipRegionColumn" Binding="{Binding ShipRegion}" Header="Ship Region" Width="SizeToHeader"/><DataGridTextColumn x:Name="shipViaColumn" Binding="{Binding ShipVia}" Header="Ship Via" Width="SizeToHeader"/></DataGrid.Columns></DataGrid><Button x:Name="btnSave" Content="Save" Height="41" Margin="0,10,10,0" VerticalAlignment="Top" Click="btnSave_Click" HorizontalAlignment="Right" Width="162"/></Grid></Window>


When I click in the datagrid to add a new row, I will like for the customer Id column to automatically take the value of the list so that the customer doesn't have to.

Is there any way to do this?


Guisselle


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>