Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF SelectionChange Event for ComboBox with Binding from database crashes the Program
    primarykey
    data
    text
    <p>I've got a big issue trying to modify the ItemSource for a ComboBox. When I try to debug the app by putting a breakpoint on MessageBox.Show(...) in the Sub cboTest_SelectionChanged (see below), I get this error:</p> <pre><code>An Unhandled exception of type 'System.InvalidCastException' occured in Data.exe Additional Information: Unable to cast object of type 'System.Data.DataRowView' to type 'System.Windows.Controls.ComboBoxItem' </code></pre> <p>How can I fix this?</p> <p>Here's what I do. I create an ACCESS 2007 Database named SNIGDoFFE.accdb, my solution is named <strong>Data</strong>, I added the Database through the <strong>Add New Source Configuration wizard</strong> and I drag and drop the Field <strong>"Libelle"</strong> from the Table <strong>Ethnie</strong> to the the ComboBox, so it modifies my files like this:</p> <p>In MainWindow.xaml.vb</p> <pre><code>Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles MyBase.Loaded Dim SNIGDoFFEDataSet As Data.SNIGDoFFEDataSet = CType(Me.FindResource("SNIGDoFFEDataSet"), Data.SNIGDoFFEDataSet) 'Load data into the table Ethnie. You can modify this code as needed. Dim SNIGDoFFEDataSetEthnieTableAdapter As Data.SNIGDoFFEDataSetTableAdapters.EthnieTableAdapter = New Data.SNIGDoFFEDataSetTableAdapter.EthnieTableAdapter() SNIGDoFFEDataSetEthnieTableAdapter.Fill(SNIGDoFFEDataSet.Ethnie) Dim EthnieViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("EthnieViewSource"), System.Windows.Data.CollectionViewSource) 'EthnieViewSource.View.MoveCurrentToFirst 'I comment this line to leave the combobox empty End Sub </code></pre> <p>The code for cboTest_SelectionChanged is:</p> <pre><code>Private Sub cboTesT_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles cboTesT.SelectionChanged MessageBox.Show(CType(cboTesT.SelectedItem, ComboBoxItem).Content) End Sub </code></pre> <p>In XAML:</p> <pre><code>&lt;ComboBox x:Name="cboTest" width="120" Margin"184,10,183,134" SelectionChanged="cboTest_SelectionChanged" DisplayMemberPath="Libelle" ItemSource="{Binding Source={StaticResource EthnieViewSource}}" /&gt; </code></pre> <p>In my Resources:</p> <pre><code>&lt;Window.Resources&gt; &lt;local:SNIGDoFFEDataSet x:Key"SNIGDoFFEDataSet" /&gt; &lt;CollectionViewSource x:Key="EthnieViewSource" Source="{Binding Ethnie, Source={StaticResources SNIGDoFFEDataSet}}" /&gt; &lt;/Window.Resources&gt; </code></pre> <p><strong>The Solution is built correctly, the main Window loaded correctly, but when I select an item in the combobox, the program crashes. I try to remove all binding and create Item with and everything work, but by binding the source from my database, it crashes again.</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload