Out of range error on Datagrid view

gixxerrider73

New member
Joined
Nov 18, 2012
Messages
4
Programming Experience
Beginner
I have a windows for with a Datagridview. I use the code below with check boxes to show/hide the columns. There are 14 columns and it works fine except for columns index 0, 1, 3. the code is exactly the same for all of the columns. Any thoughts?


This is the code the that throws the error

Private
Sub SysCheckbox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SysCheckbox.CheckedChanged

If SysCheckbox.Checked Then
DataGridView1.Columns(0).Visible = true

Else
DataGridView1.Columns(0).Visible = false


End If

End Sub







This is the error

System.InvalidOperationException was unhandled
Message="An error occurred creating the form. See Exception.InnerException for details. The error is: Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"
Source="HyperMedia"
StackTrace:
at HyperMedia.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
at HyperMedia.My.MyProject.MyForms.get_Tab()
at HyperMedia.My.MyApplication.OnCreateMainForm() in D:\Jay_Data\Arcade\Software Develope\HyperMedia\HyperMedia\My Project\Application.Designer.vb:line 35
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at HyperMedia.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.ArgumentOutOfRangeException
Message="Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"
Source="mscorlib"
ParamName="index"
StackTrace:
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.DataGridViewColumnCollection.get_Item(Int32 index)
at HyperMedia.Tab.SysCheckbox_CheckedChanged(Object sender, EventArgs e) in D:\Software Develope\HyperMedia\HyperMedia\Tab.vb:line 625
at System.Windows.Forms.CheckBox.OnCheckedChanged(EventArgs e)
at System.Windows.Forms.CheckBox.set_CheckState(CheckState value)
at HyperMedia.Tab.InitializeComponent() in D:\Software Develope\HyperMedia\HyperMedia\Tab.Designer.vb:line 503
at HyperMedia.Tab..ctor()
InnerException:
 
You get the exception because there is no column at index zero. As you can see from the stack trace of the exception, that event handler is being executed during the creation of the form. The CheckBox is created, configured and added to the form during that creation and part of that configuration is setting the Checked property to True. That change raises the CheckedChanged event but the DataGridView has not had any columns added at that point so the exception is thrown. You have a couple of main options to fix it:

1. Make sure that the CheckBox doesn't have its Checked property changed until the grid has columns, e.g. do it in code in the Load event handler.

2. Add some code to that CheckChanged event handler so that it tests whether there is a column at index zero before trying to access it.
 
no idea how to code option 1.
You would not set the Checked property to True in the designer but instead do it in code in the Load event handler of the form.
 

Latest posts

Back
Top