Avatar of Olukayode Oluwole
Olukayode Oluwole
Flag for Canada

asked on 

How Do I change a Cast Statement made from a Generic List to another List made from a Binding List

I have a C# application in which i loaded a Generic List to a  Grid Column using the script below. The Load  of the List worked.

dgvcmbarsaleditemid.DataSource = GlobalConfig.Connection.GetInventoryItems_All();
dgvcmbarsaleditemid.DisplayMember = "FullInventoryList";
dgvcmbarsaleditemid.ValueMember = "INVITEMId";

The Cast statement  below  also worked  perfectly

var result2 = ((List<InventoryItemModel>)dgvcmbarsaleditemid.DataSource).Find(x => x.INVITEMId == Convert.ToString(dgvDetailsTable.CurrentRow.Cells["dgvcmbarsaleditemid"].Value));

However I have  changed my mode of loading the same Column by using a Binding List.
And the List also loaded properly in the Grid. The code is shown below

BindingList<InventoryItemModel> inventoryitems = new BindingList<InventoryItemModel>();  // for new method


var parameter07 = new { companycodex = LoginDetails.staticcompany };
var newList7 = NewDataAccess.ReadData<InventoryItemModel>("public.spinventory_getall", parameter07);
dgvcmbarsaleditemid.DataSource = null;
dgvcmbarsaleditemid.DataSource = inventoryitems;
dgvcmbarsaleditemid.DisplayMember = "FullInventoryList";
dgvcmbarsaleditemid.ValueMember = "INVITEMId";
inventoryitems.Clear();                  // This line clears the customers list box
newList7.ForEach(x => inventoryitems.Add(x));

Now  my Cast statement now reports an error that the Cast Statement Using a Generic List
is now invalid because the List is prepared as a Binding List  ( see   error  below )


The Line commented out worked when i had a Generic List

What modification do i need to make the Cast statement valid now that i have a Binding List

Thanks

Olukay


* GridC#

Avatar of undefined
Last Comment
Olukayode Oluwole

8/22/2022 - Mon