2014-03-01 42 views
-1

试图找出如何防止此错误?DataGridView System.IndexOutOfRangeException

当我点击添加按钮项添加到集合 后来我在一个单元格点击,我得到这个错误:

System.IndexOutOfRangeException是未处理 消息:类型的未处理的异常“System.IndexOutOfRangeException '发生在System.Windows.Forms.dll 附加信息:索引-1没有值。

Public Class Form1 

     Private temps As IList(Of TemplateInfo) = New List(Of TemplateInfo) 

     Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click 

      Dim ti As New TemplateInfo 
      ti.Name = "Test" + DateTime.Now.ToString 
      ti.FileName = "Bla" 
      dgInterfaces.DataSource = Nothing 
      temps.Add(ti) 
      dgInterfaces.DataSource = temps 
     End Sub 

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
      dgInterfaces.DataSource = temps 

     End Sub 
    End Class 


Public Class TemplateInfo 

    Private m_Name As String 
    Private m_FileName As String 

    Public Property Name As String 
     Get 
      Return m_Name 
     End Get 
     Set(value As String) 
      m_Name = value 
     End Set 
    End Property 

    Public Property FileName As String 
     Get 
      Return m_FileName 
     End Get 
     Set(value As String) 
      m_FileName = value 
     End Set 
    End Property 

End Class 

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ 
Partial Class Form1 
    Inherits System.Windows.Forms.Form 

    'Form overrides dispose to clean up the component list. 
    <System.Diagnostics.DebuggerNonUserCode()> _ 
    Protected Overrides Sub Dispose(ByVal disposing As Boolean) 
     Try 
      If disposing AndAlso components IsNot Nothing Then 
       components.Dispose() 
      End If 
     Finally 
      MyBase.Dispose(disposing) 
     End Try 
    End Sub 

    'Required by the Windows Form Designer 
    Private components As System.ComponentModel.IContainer 

    'NOTE: The following procedure is required by the Windows Form Designer 
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor. 
    <System.Diagnostics.DebuggerStepThrough()> _ 
    Private Sub InitializeComponent() 
     Me.dgInterfaces = New System.Windows.Forms.DataGridView() 
     Me.btnAdd = New System.Windows.Forms.Button() 
     CType(Me.dgInterfaces, System.ComponentModel.ISupportInitialize).BeginInit() 
     Me.SuspendLayout() 
     ' 
     'dgInterfaces 
     ' 
     Me.dgInterfaces.AllowUserToAddRows = False 
     Me.dgInterfaces.AllowUserToDeleteRows = False 
     Me.dgInterfaces.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize 
     Me.dgInterfaces.Location = New System.Drawing.Point(12, 60) 
     Me.dgInterfaces.Name = "dgInterfaces" 
     Me.dgInterfaces.ReadOnly = True 
     Me.dgInterfaces.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect 
     Me.dgInterfaces.ShowRowErrors = False 
     Me.dgInterfaces.Size = New System.Drawing.Size(391, 143) 
     Me.dgInterfaces.TabIndex = 14 
     ' 
     'btnAdd 
     ' 
     Me.btnAdd.Location = New System.Drawing.Point(12, 31) 
     Me.btnAdd.Name = "btnAdd" 
     Me.btnAdd.Size = New System.Drawing.Size(75, 23) 
     Me.btnAdd.TabIndex = 15 
     Me.btnAdd.Text = "Add" 
     Me.btnAdd.UseVisualStyleBackColor = True 
     ' 
     'Form1 
     ' 
     Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) 
     Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font 
     Me.ClientSize = New System.Drawing.Size(478, 262) 
     Me.Controls.Add(Me.btnAdd) 
     Me.Controls.Add(Me.dgInterfaces) 
     Me.Name = "Form1" 
     Me.Text = "Form1" 
     CType(Me.dgInterfaces, System.ComponentModel.ISupportInitialize).EndInit() 
     Me.ResumeLayout(False) 

    End Sub 
    Friend WithEvents dgInterfaces As System.Windows.Forms.DataGridView 
    Friend WithEvents btnAdd As System.Windows.Forms.Button 

End Class 
+0

LOL;超过1000次观看并且没有得票?我相信这必须帮助至少几个人...... – eschneider

回答

5

您需要在添加后刷新CurrencyManager。

​​
相关问题