Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

BindingSource Control Throwing ArgumentOutOfException When Changing DataSource

$
0
0

I have a Winforms prgram that is using BindingSource controls to bind a collection of custom objects.  When I either bind to an empty collection or move from a collection that has objects to an empty collection, the live of code where I set the BindingSource DataSource to the BindingList throws an ArgumentOutOfException stating:

"Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index"

I have tried setting the BindingSource control DataSource to null before setting the DataSurce to the BindingList in order to clear out he previous bindings, thinking they were corrupting the binding process.  But, that did me no good.  My code is below:

private void LoadPGDetails()
        {
            PGAccountContractDetail contract = (PGAccountContractDetail)ContractsBindingSource.Current;

            List<PGDetail> pgDetails = new List<PGDetail>();

            if (contract != null)
            {
                contract.PGDetails = new BOPGDetail().GetPGDetails(contract.PGAccountContractDetailID);
            }
            else
            {
                contract.PGDetails = new List<PGDetail>();
            }

            var pgDetailsBindingList = new BindingList<PGDetail>(contract.PGDetails);
            this.pGDetailsBindingSource.DataSource = null;
            this.pGDetailsBindingSource.DataSource = pgDetailsBindingList;

            pGDetailsDataGridView.Refresh();

            if (_initialLoad)
            {
                BindPGControls();
            }
        }
At this point I am stumped.  Does anybody out there have any ideas or guidance?  Thanks in advance.


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>