Working On Simple Entity Frame Work sample with Combo Box . i am trying to populate datagridview on the basis of combo Box selections but i am getting casting error
I have Simple MDF database and i used database first to generate model for EF .
in EF , NiftyFO is a model consists of TradId, StrikePrice , VolumeTraded etc ..
NiftyEntities nf = new NiftyEntities();
NiftyFO nfo = new NiftyFO();
nfo = (NiftyFO)(comboBox1.SelectedItem);
var result = from x in nf.NiftyFOes
where x.StrikePrice ==nfo.StrikePrice
select x;
dataGridView1.DataSource = result;
dataGridView1.Refresh();
And the exact error it showing wheni change ComboBox Seletion is
Unable to cast object of type '<>f__AnonymousType0`1[System.Nullable`1[System.Decimal]]
to type 'agileLocal.NiftyFO.'
and
Combo Box Filled with Strike Price and loaded from the databse throgh the ef and code goes like this
NiftyEntities nf = new NiftyEntities();
var result2 = from x in nf.NiftyFOes
select new { x.StrikePrice };
//comboBox1.DisplayMember = "StrikePrice";
comboBox1.DataSource = result2;
Solved :
select new { x.StrikePrice };
i just changed this line
select x.StrikePrice . and it worked .
then your code will be
because in your
select new { x.StrikePrice };you are selecting theStrikePriceand not the object it self