Click here to Skip to main content
15,117,925 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
hey guys
i have wpf Data Grid with below columns

IDStuff(int), Title(string), Type(string),  InitialBalance(int)

and below class
class stuffdt

    public int IDStuff { set; get; }
    public string Title { set; get; }
    public string Type { set; get; }
    public int InitialBalance { set; get; }


now i write this code

private void dtstuff_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
      List<cellprod> cp = new List<cellprod>();
      stuffdt sd =(stuffdt)this.dtstuff.SelectedItem;

but i have this error

Unable to cast object of type '<>f__AnonymousType1`4[System.Int32,System.String,System.String,System.Int32]' to type 'stuffdt'.
Clifford Nelson 10-Aug-13 14:39pm
There is not enough information to figure out what your problem is. I would put a breakpoint in in the doubleclick and see what your situation really is

aref.bozorgmehr 10-Aug-13 17:39pm
i fill may data grid with linq to entity when i use your link solution my result is null
Maciej Los 10-Aug-13 17:42pm
See updated answer ;)
aref.bozorgmehr 10-Aug-13 18:10pm
you are fantastic so thanks
Maciej Los 10-Aug-13 18:13pm
Thank you and you're welcome ;)
you cannot typecast dynamic objects. the two have to be identical for them to be mapped correctly.

i'd suggest you try using the listbox itemsource to retrieve the records that you want. something


private void dtstuff_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)

stuffdt sd = {DataGridItemSource}[dtstuff.SelectedIndex]; 

//assuming the datagriditemsource is a list of type stuffdt

aref.bozorgmehr 10-Aug-13 15:32pm
i have this Error The name 'DataGridItemSource' does not exist in the current context
Member 9549287 10-Aug-13 15:39pm
the " { ... } " meant you had to replace the entire {DataGridItemSource} with the variable or view model where your data grid is getting it's values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900