Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7644879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:47:43+00:00 2026-05-31T09:47:43+00:00

I’m getting the strangest error I am absolutely clueless about. I’ll post a description

  • 0

I’m getting the strangest error I am absolutely clueless about. I’ll post a description along with some code here, and hopefully one of you guys can point me in the right direction.

My app (Winforms), allows a user to add items to a datagridview (bound to a list), and everytime an item is added, the list is serialized to a xml file. When the application is initially launched, the program checks for the xml file, and if found, adds the previously added items to the dgv.

Ive also added a DataGridViewButtonColumn to delete items from the dgv (list). Here is some of the code.

Main class:

 static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new formFoldingClient());
        }

The form’s constructor calls this method to initially set up the dgv

private void InitialDataGridViewSetup()
        {
            dgvClients.DataSource = null;

            //adding delete button column
            DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn();
            btnDelete.Name = "btnDelete";
            btnDelete.Text = "Delete";
            btnDelete.HeaderText = "Delete";
            btnDelete.UseColumnTextForButtonValue = true;
            btnDelete.DefaultCellStyle.BackColor = Color.DarkBlue;
            btnDelete.DefaultCellStyle.ForeColor = Color.White;
            dgvClients.Columns.Add(btnDelete);

            RefreshDataGridView();
        }

Everytime an item is added or removed, the dgv is refreshed by calling this method:

 private void RefreshDataGridView()
            {
                dgvClients.DataSource = null;

                if (clientList.Count != 0)
                {
                    dgvClients.DataSource = clientList;
                    dgvClients.Show();
                    dgvClients.ClearSelection();


                }
            }

Method that gets triggered when Delete button on a row in the dgv is pressed, followed by the method the performs the delete

 private void dgvClients_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0) //delete button has been clicked
            {
                DeleteClient(dgvClients.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].FormattedValue.ToString());
            }
        }

        private void DeleteClient(string clientToDelete)
        {
            dgvGrid.DataSource = null;
            int removeAt = new int();

            for (int i=0; i<clientList.Count; i++)
            {
                if (clientList[i]._ClientName == clientToDelete)
                {
                    removeAt = i;
                    break;
                }
            }

            clientList.RemoveAt(removeAt);
            LogToFile("Removed client: " + clientToDelete);
            LogToBox("Removed client: " + clientToDelete);
            RefreshDataGridView();
            SaveConfigAsXml();
            LogToFile("Changes after deletion persisted to clients.xml.");

        }

I believe this is all the code that should be necesarry. If you need anymore, do let me know.

Problem brief
When the app first loads if it finds the xml and loads those items to the list, everything performs as expected. I can add more items, delete all items (one at a time) etc.

However, if I start without an initial xml, adding items is not a problem. But when I delete the last remaining item in the dgv, I get the following exception at the last line of Main()

Index out of range Exception: {"Index -1 does not have a value."}

Stack Trace

at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
   at System.Windows.Forms.CurrencyManager.get_Current()
   at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
   at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)
   at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
   at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown)
   at System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)
   at System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.DataGridView.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at FoldingMonitorLocalClient.Program.Main() in C:\Users\xbonez\Documents\Visual Studio 2010\Projects\FoldingClient\FoldingClient\Program.cs:line 17
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

More Info
So, I just realized that if I have n items in the dgv, deleting just the first item also causes the same exception. Deleting items 2 to n is no problem.

Code that reads xml and adds to list

 private void ReadFromConfigFile()
        {
            LogToFile("Beginning to read from clients.xml.");

            XmlSerializer deserializer = new XmlSerializer(typeof(List<Client>));

            try
            {
                List<Client> tempClientList = new List<Client>();
                using (Stream reader = new FileStream("clients.xml", FileMode.Open))
                {
                    tempClientList = ((List<Client>)deserializer.Deserialize(reader));
                }

                foreach (Client client in tempClientList)
                {
                    clientList.Add(client);
                }
            }
            catch (FileNotFoundException ex)
            {
                //config file does not exist
                this.LogToBox("No saved settings found.");
                this.LogToFile("No existing clients.xml present.", ex);
            }
            catch (Exception ex)
            {
                LogToBox("Unable to load saved settings. Please see log for more details.");
                LogToFile("Failed to read clients.xml.", ex);
            }
            finally
            {
                LogToFile("Finished reading clients.xml.");
            }
        }

Code when add button is clicked

private void btnAdd_Click(object sender, EventArgs e)
        {
            this.tbxClientName.BackColor = Color.White;
            this.tbxLogLoc.BackColor = Color.White;

            bool exists = false;

            foreach (Client client in clientList)
            {
                if (client._ClientName == this.tbxClientName.Text)
                    exists = true;
            }

            if (String.IsNullOrEmpty(tbxClientName.Text))
            {
                this.tbxClientName.BackColor = Color.Yellow;
                LogToBox("Enter Client Name");
                LogToFile("user attempted to add client without specifying client name.");
            }
            else if (String.IsNullOrEmpty(tbxLogLoc.Text))
            {
                this.tbxLogLoc.BackColor = Color.Yellow;
                LogToBox("Select WorkLog location.");
                LogToFile("User attempted to add client without specifying worklog location.");
            }
            else if (exists)
            {
                //client name entered by user already exists
                LogToBox("Client name " + this.tbxClientName.Text + " already exists. Enter another Client name.");
                LogToFile("Duplicate client name entered.");
                this.tbxClientName.BackColor = Color.Yellow;
            }
            else
            {
                //everything is valid. Add new client to list
                clientList.Add(new Client(tbxClientName.Text, tbxLogLoc.Text));
                LogToBox("Added new client: " + tbxClientName.Text + ".");
                LogToFile("Added new client: " + tbxClientName.Text + ".");

                this.tbxClientName.Text = String.Empty;
                this.tbxLogLoc.Text = String.Empty;

                RefreshDataGridView();
                SaveConfigAsXml();
            }            
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:47:45+00:00Added an answer on May 31, 2026 at 9:47 am

    This seems to be some kind of internal binding bug in .NET.
    I experienced exactly the same exception whenever using DataGridView bound to a List.
    I really spent a lot of time trying to find a solution and I’ve finally managed to get rid of these exceptions today – by adding ICurrencyManagerProvider interface to all my Lists.
    This interface only has a “CurrencyManager” read-only property and a “GetRelatedCurrencyManager” method.
    I’m just returning Nothing in both of them and that’s it, no more CurrencyManager “index -1 does not have a value” stuff.

    EDIT: OK, just found out the “proper way” is actually to use BindingList(of T) class instead of List(of T)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.