Select a listview item as object

.net c# dynamic-linq linq xaml


I got a ListView control that contains a GridView control too. Data are comming from different Table from database Database scheme:

  1. Books (BookID, BookName, ISBN, CopyLeft);
  2. Borrowers (BorrowerID, BorrowerName, BorrowerLevel);
  3. Transactions (TransactionID, BorrowerID, BookID, BorrowDate, ReturnDate, IsReturned);

The corresponding Class are Book, Borrower and Transaction respectively. GridView fetches TransactionID, BorrowerID, BorrowerName, BookName, BorrowDate and ReturnDate from these tables using data binding. XAML Code:

    <ListView Margin="15,57,58,57" Name="borrowedBookList" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding }" KeyDown="borrowedBookList_KeyDown">
                    <GridViewColumn  Width="80" Header="Borrower ID" DisplayMemberBinding="{Binding Path=BorrowerID}"/>
                    <GridViewColumn Width="220" Header="Borrower Name" DisplayMemberBinding="{Binding Path=BorrowerName}"/>
                    <GridViewColumn Width="220" Header="Book Name" DisplayMemberBinding="{Binding Path=BookName}"/>
                    <GridViewColumn Width="100" Header="Date" DisplayMemberBinding="{Binding Path=BorrowDate}"/>
                    <GridViewColumn Width="100" Header="Return Date" DisplayMemberBinding="{Binding Path=ReturnDate}"/>
    <Label Content="Borrowed Book List" Height="34" HorizontalAlignment="Left" Margin="310,12,0,0" Name="label1" VerticalAlignment="Top" Width="151" />
    <Button Content="Close" Height="31" HorizontalAlignment="Left" Margin="636,535,0,0" Name="close" VerticalAlignment="Top" Width="106" Click="close_Click"/>
    <Button Content="Return This Book" Height="31" HorizontalAlignment="Left" Margin="454,535,0,0" Name="return" VerticalAlignment="Top" Width="115" Click="return_Click"/>

Now, can I select a row as an object of Transaction or Book or Borrower? My objective is to select a row and identify the corresponding entry of the Transactions, Books tables and increment the CopyLeft and assign "YES" into IsReturned attribute. So how can I do this?

In addition: I tried to code like this:

var selectedTran = this.borrowedBookList.selectedItem as Transaction;
var selectedBook=this.borrowedBookList.selectedItem as Book;
catch(Exception ex)

The exception shows that "Object reference not set to an instance of an object." Then i tried to find the type of the borrowedBookList like this:


which shows an anonymous Type.

9/18/2012 9:33:58 AM

Accepted Answer

Your query probably results anonymus type:

    var query = from b in ctx.books 
                select new 
                { BorrowerID = b.ID, BorrowerName = b.Name , ... 

Your grid's selectedItem property contains this type, not your original entity classes. So i would use a named class where you should include primary key IDs, and update the corresponding entities using primary keys from the selected item:

public class ForYourGrid
    public int BorrowerID { get; set; }
    public string BorrowerName { get; set; }

    var query2 = from b in ctx.books 
                select new ForYourGrid 
                { BorrowerID = b.ID, BorrowerName = b.Name , ... 

    var selected = this.borrowedBookList.selectedItem as ForYourGrid;

    var selectedTran = ctx.Transactions.SingleOrDefault(e => e.TransactionID == selected.TransactionID);
    selectedTran.isReturned = "YES";

    var selectedBook = ctx.Books.SingleOrDefault(e => e.BookID == selected.BookID);

9/18/2012 3:46:11 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow