2014-03-01 74 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。

​​
相關問題