none
Out of Index Error Can someone point me in the right direction to fix this error? RRS feed

  • Question

  • Problem getting Account Master Header Data

    Source:mscorlib

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

    Parameter name:index

    Stack Trace: at System.Collection.ArrayList.get_Item(Int32 index) at System.Windows.Forms.DataGrid.ViewRowCollection.SharedRow(Int32rowIndex)

    at System.Windows.Forms.DataGridViewRowCollection.get_Item(Int32 index)

    at PgMgmt.frmMain.OnLoad(EventArgs e)

    code


                // Load AccountMaster Header Data
                try
               {
                    var accountMastersBindingList = new BindingList<AccountMaster>(_accountMasters);
                    this.AccountMastersBindingSource.DataSource = accountMastersBindingList;
                    accountMasterDataGridView.DataSource = this.AccountMastersBindingSource;

                    accountMasterDataGridView.Rows[0].Selected = true;
                    DataGridViewRow rowAccountMaster = accountMasterDataGridView.SelectedRows[0];

                    LoadGroups();

                    groupMastersDataGridView.Rows[0].Selected = true;
                    DataGridViewRow rowGroupMaster = groupMastersDataGridView.SelectedRows[0];

                    String groupMasterID = rowGroupMaster.Cells[0].Value.ToString();

                    LoadCoverages();
                   
               }
               catch (Exception ex)
               {
                  MessageBox.Show("Problem getting Account Master Header Data\r\n" + "Source: " + ex.Source + "\r\n" + "Description: " + ex.Message + "\r\n" + "Stack Trace: " + ex.StackTrace);
               }


    Wednesday, September 18, 2019 4:00 PM

All replies

  • Only you know which line of code this occurred on, and you didn't show us.  The implication here is that either accountMasterDataGridView or groupMastersDataGridView was empty.  In that case, referring to element [0] would be out of range.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, September 18, 2019 4:50 PM
  • It seems to me that the whole block needs to be in an if-statement on rows.count > 0

    Indicating that there is one or more items on the grid. If row.count = 0, then there is no item in the grid and grid.rows[0] is going to throw the exception trying to access the item at the 0 index, since there are no items in the grid.

    Sorry about the post and the blank lines, but I am posting from a phone and the keyboard has no Ctrl/z or delete-key.🙂

    Wednesday, September 18, 2019 5:59 PM
  • Hi Si2323, 

    Thank you for posting here.

    According to your question, I try to make a test but I need more information to reproduce your problem.

    Could you provide more details about your exception with which line of the code thrown the exception and some code about ‘AccountMaster’, ‘_accountMasters’, ‘LoadGroups()’ and ‘LoadCoverages()’? It will help us to do the test.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 19, 2019 7:51 AM
    Moderator