title: C# Why does the column index change at random on my DataGridView within tab control
body: <p>I have a really strange problem. I have a tab control on a Windows Form with 3 tabs. Each tab has an identical DataGridView. I have a button in column 5 on each of the DataGridViews. So identical tabs, identical GridViews, vertually identical code to populate each, which looks like that below.</p>
  
  <p>However, the column index works exactly as expected for the first tab i.e zero indexed. But for the second and third tab, it appears to see the first column as index , err, -1 i guess as it doesn't see it. But here's the really strange, and annoying part....it seems to change at random. Sometimes when i run the program it works as expected and takes the first column as index 0, and sometimes it displays the strange behaviour as described by completely ignoring the first column and counts the second column as Index 0. This is a huge problem as i take the values from the cells in the columns to populate another Form - which the button in column 5 opens.</p>
  
  <pre><code>private void populateToolsDataGrid()
          {
             doc = XDocument.Load(XMLfilePath + "\\DataFile\\ILS_Support_TOOLS.xml"); // this works but does not allow sorting
              var xmlData = from supeq in doc.Elements("data").Elements("supequi").Elements("tool")
                            select new
                            {
                                Name = supeq.Element("toolName").Value,
                                NSN = supeq.Element("toolNSN").Value,
                                PN = supeq.Element("toolPN").Value,
                                Cage = supeq.Element("toolCage").Value,
                                ID = supeq.Attribute("id").Value
                            };
              GridViewTools.DataSource = xmlData.ToList();
              GridViewTools.Columns["ID"].Visible = false;
  
              if (GridViewTools.Columns.Contains("detailsButton") == false)
              {
                  DataGridViewButtonColumn button = new DataGridViewButtonColumn();
                  {
                      button.Name = "detailsButton";
                      button.HeaderText = "Details";
                      button.Text = "Details";
                      button.UseColumnTextForButtonValue = true; 
                      GridViewTools.Columns.Add(button);
  
                  }
              }
  
              if (toolDetailsButtonInitialised == false)
              {
                  GridViewTools.CellClick += dataGridViewTools_CellClick; // click event for the button click
                  toolDetailsButtonInitialised = true;
              }
  
              GridViewTools.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  
          }
  }
  </code></pre>