Note that there are some explanatory texts on larger screens.

plurals
  1. POWpf selecteditem not working - Linq
    primarykey
    data
    text
    <p>i have one grid binded with some data, coming from database as bellow and one edit button</p> <pre><code>&lt;DataGrid AutoGenerateColumns="False" Name="SParts_grid" HorizontalAlignment="Center" Margin="32,101,32,0" VerticalAlignment="Top" Height="187" Width="530" &gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="Part No" Binding="{Binding Path=SPartID}" /&gt; &lt;DataGridTextColumn Header="Part Code" Width="85" Binding="{Binding Path=SPartCode}" /&gt; &lt;DataGridTextColumn Header="Part Name" Width="160" Binding="{Binding Path=SPartName}" /&gt; &lt;DataGridTextColumn Header="Model" Width="120" Binding="{Binding Path=ModelName}" /&gt; &lt;DataGridTextColumn Header="Location" Binding="{Binding Path=SPartLocation}" /&gt; &lt;DataGridCheckBoxColumn Header="Active" Width="58" Binding="{Binding Path=SPartActive}" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;Button x:Name="btnEdit" Content="Edit" HorizontalAlignment="Left" Margin="105,323,0,0" VerticalAlignment="Top" Width="75" Click="btnEdit_Click"/&gt; </code></pre> <p>all data fetched from db table called <code>TblSpareParts</code> just one column <code>"ModelName"</code> is from another table called <code>TblBikeModels</code></p> <p>so my code is below to fetch data</p> <pre><code>window loaded function private void Window_Loaded(object sender, RoutedEventArgs e) { LoadParts(); } private void LoadParts() { RST_DBDataContext conn = new RST_DBDataContext(); var AllPArts = (from s in conn.TblSpareParts join m in conn.TblBikeModels on s.ModelID equals m.ModelID select new { s.SPartName, s.SPartCode, s.SPartLocation, s.SPartID, m.ModelName }).ToArray(); SParts_grid.ItemsSource = AllPArts; } </code></pre> <p>it works well but now if i need the selecteditem it does not work as below</p> <pre><code>private void btnEdit_Click(object sender, RoutedEventArgs e) { TblSparePart SelectedSPData = SParts_grid.SelectedItem as TblSparePart; if (SelectedSPData == null) { MessageBox.Show("You Must Select a Part"); } else { MessageBox.Show("Selected"); } } </code></pre> <p>but when i use this <code>LoadParts</code> function then <code>selecteditem</code> works fine but it does not show the data in <code>ModelName</code> column</p> <pre><code>private void LoadParts() { RST_DBDataContext conn = new RST_DBDataContext(); List&lt;TblSparePart&gt; AllPArts = (from s in conn.TblSpareParts select s).ToList(); SParts_grid.ItemsSource = AllPArts; } </code></pre> <p>Basically problem is in <code>LoadParts</code> function</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.
 

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