Reputation: 181
I have a database with three tables. Also I have a datagrid which shows several properties of two entities(tables). To select information from both tables I use a stored procedure which works fine. I want to select a row in this datagrid and by pressing a button this will be added into the third table in database. And another button should be for showing the whole information (all properties) of the entities. I don't know what to do with X after ChosenBooksAuthors - it should be BookID insted of X but I can't reach it, what should I do? How to add selected row into a table? XAML:
<Canvas>
<DataGrid CanUserAddRows="False" x:Name="Info" SelectedItem="{Binding Path=ChosenBooksAuthors}" ItemsSource="{Binding Path=Bookslist}" Background="Honeydew" Canvas.Top="190" Canvas.Left="38" Width="297" Height="105" ></DataGrid>
</Canvas>
C#:
private List<Book> _VMBooksAuthors = new List<Book>();
public List<Book> ChosenBooksAuthors
{
get
{
return _VMBooksAuthors;
}
set
{
_VMBooksAuthors = value;
OnPropertyChanged("ChosenBooksAuthors");
}
}
private void BasketButton_Click(object sender, RoutedEventArgs e)
{
Basket b = db1.BasketSet.Where(c => c.BookID == ChosenBooksAuthors.X).FirstOrDefault();
db1.BasketSet.Add(b);
db1.SaveChanges();
}
Upvotes: 0
Views: 57
Reputation: 12314
ChosenBooksAuthors is a collection, selected item should be Book. Try:
<DataGrid CanUserAddRows="False" x:Name="Info" SelectedItem="{Binding Path=Book}" ...
Or just cast:
private void BasketButton_Click(object sender, RoutedEventArgs e)
{
var book = (Book)myDataGrid.SelectedItem;
Basket b = db1.BasketSet.FirstOrDefault(c => c.BookID == book.BookID); // No need for where with FirstOrDefault()
db1.BasketSet.Add(b);
db1.SaveChanges();
}
Upvotes: 1