DataGridView仅使用一个文件抛出System.invalidoperationException

时间:2014-04-15 06:24:29

标签: vb.net datagridview

我有一个Datagridview,我在reading文件的构造函数中初始化文件Widerstand.txt中的单元格。这很好。

   Public Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        Widerstand.widerstandEinlesen()
        DataGridView1.Rows.Clear()
        For i = 0 To Widerstand.wertepaarGeschwindigkeit.Count - 1
            DataGridView1.Rows.Add()
            DataGridView1.Rows(i).Cells(0).Value = Widerstand.wertepaarGeschwindigkeit.Item(i)
            DataGridView1.Rows(i).Cells(1).Value = Widerstand.wertepaarWiderstand.Item(i)
        Next


    End Sub

当我点击一个按钮时,我想将datagridview的所有单元格添加到文件中,以防某些内容发生变化。这就是我试图这样做的方式:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Widerstand.widerstandEintragen()
 End Sub

Shared Sub widerstandEintragen()
        Dim datei As New FileStream(Application.StartupPath + "\Widerstand\Widerstand.txt", FileMode.Open)
        Dim schreiber As New StreamWriter(datei)
        For i = 0 To FormWiderstand.DataGridView1.RowCount - 2
            schreiber.WriteLine(FormWiderstand.DataGridView1.Rows(i).Cells(0).Value & ";" & vbTab & FormWiderstand.DataGridView1.Rows(i).Cells(1).Value & ";")
        Next
        schreiber.Close()
        datei.Close()
    End Sub

但我得到了System.invalidoperationException   For i = 0 To FormWiderstand.DataGridView1.RowCount - 2。 奇怪的是,如果我将文件更改为Widerstandblabla.txt就可以了。

任何人都可以帮助我吗? THX!

编辑:这是导致此问题的类的完整代码。

Imports System.IO
Public Class Widerstand
    Public Shared widerstand As New List(Of Double)
    Public Shared wertepaarGeschwindigkeit As New List(Of Double)
    Public Shared wertepaarWiderstand As New List(Of Double)

    Shared Sub widerstandEinlesen()
        widerstand.Clear()
        wertepaarGeschwindigkeit.Clear()
        wertepaarWiderstand.Clear()
        Dim datei As New FileStream(Application.StartupPath + "\Widerstand\Widerstand.txt", FileMode.Open)
        Dim leser As New StreamReader(datei)
        Dim zeile As String
        Dim werte() As String
        Dim wertepaarMaxGeschwindigkeit As Double

        While leser.Peek <> -1
            zeile = leser.ReadLine
            werte = zeile.Split(";")
            wertepaarGeschwindigkeit.Add(werte(0))
            wertepaarWiderstand.Add(werte(1))
            If werte(0) > wertepaarMaxGeschwindigkeit Then
                wertepaarMaxGeschwindigkeit = werte(0)
            End If
        End While
        leser.Close()
        datei.Close()
        For i = 0 To wertepaarMaxGeschwindigkeit
            For j = 0 To wertepaarWiderstand.Count - 1
                If wertepaarGeschwindigkeit.Item(j) >= i Then
                    Dim r0, r1, v0, v1 As Double
                    If j <> 0 Then
                        r0 = wertepaarWiderstand.Item(j - 1)
                        v0 = wertepaarGeschwindigkeit.Item(j - 1)
                    Else
                        r0 = 0
                        v0 = 0
                    End If
                    r1 = wertepaarWiderstand.Item(j)
                    v1 = wertepaarGeschwindigkeit.Item(j)
                    If v1 - v0 <> 0 Then
                        widerstand.Add(r0 + (r1 - r0) / (v1 - v0) * (i - v0))
                    Else
                        widerstand.Add(0)
                    End If
                    Exit For
                End If
            Next
        Next
    End Sub

    Shared Sub widerstandEintragen(tabelle As DataGridView)
        Dim datei As New FileStream(Application.StartupPath + "\Widerstand\Widerstand.txt", FileMode.OpenOrCreate)
        Dim schreiber As New StreamWriter(datei)
        MsgBox(tabelle.RowCount)
        For i = 0 To tabelle.RowCount - 1
            schreiber.WriteLine(tabelle.Rows(i).Cells(0).Value & ";" & vbTab & tabelle.Rows(i).Cells(1).Value & ";")
        Next
        schreiber.Close()
        datei.Close()
    End Sub
End Class





Public Class FormWiderstand

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim neuesFenster As New FormHauptseite
        neuesFenster.Show()
        Me.Dispose()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Widerstand.widerstandEintragen(Me.DataGridView1)
    End Sub

    Private Sub FormWiderstand_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Widerstand.widerstandEinlesen()
        DataGridView1.Rows.Clear()
        For i = 0 To Widerstand.wertepaarGeschwindigkeit.Count - 1
            DataGridView1.Rows.Add()
            DataGridView1.Rows(i).Cells(0).Value = Widerstand.wertepaarGeschwindigkeit.Item(i)
            DataGridView1.Rows(i).Cells(1).Value = Widerstand.wertepaarWiderstand.Item(i)
        Next
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

在第一个代码段中,您只引用DataGridView1,表示网格位于当前实例上。在第二个代码段中,您使用FormWiderstand.DataGridView1,它似乎是指默认实例上的网格而不是当前实例。这是为什么始终保持一致的一个很好的例子。如果要从同一表单中的代码引用相同的网格,请以相同的方式引用它。就个人而言,我喜欢使用Me明确地引用当前实例,因为它使事情变得更加清晰。