从Form2过滤Form1中的DataGridView

时间:2016-04-01 10:08:28

标签: vb.net winforms datagridview

我在Form2(SearchWindow)中创建了一个过滤器,用于过滤Form1(frPlanMain)中找到的DataGridView中的值。

问题是我收到以下错误

Object reference not set to an instance of an object.

问题是我不确定如何在Form2中使用DataGridView(dGVPlan)。

我错过了一个字符串或什么?

Form1 的代码是:

Imports System.Data.OleDb

Public Class frPlanMain

    Public Sub goButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goButton.Click

        Dim MyPath As String = "D:\" + goTextBox.Text

        Dim cb As New OleDbConnectionStringBuilder With
            {
                .DataSource = MyPath,
                .Provider = "Microsoft.Jet.OLEDB.4.0"
            }
        cb.Add("Extended Properties", "Excel 8.0; IMEX=1; HDR=NO;")
        Dim cn As New System.Data.OleDb.OleDbConnection With
            {
                .ConnectionString = cb.ConnectionString
            }
        cn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(
            <Statement>
                    SELECT 
                        F1 As NrFir, 
                        F2 As TechSudura, 
                        F3 As TechLP, 
                        F4 As HNr, 
                        F6 As Sectiune,
                        F7 As Culoare1,
                        F8 As Culoare2,
                        F9 As Lungime,
                        F11 As PozitieStanga,
                        F14 As PozitieDreata,
                        F17 As MSpec,
                        F19 As TerminalSt,
                        F20 As GumitaSt,
                        F21 As TerminalDr,
                        F22 As GumitaDr
                    FROM 
                        [WI$]
                </Statement>.Value, cn)
        Dim dt As New DataTable
        dt.Load(cmd.ExecuteReader)
        cn.Close()
        dGVPlan.DataSource = dt

        SearchWindow.Show()

    End Sub


End Class

Form2 的代码是:

Public Class SearchWindow
    Private Sub SearchGoButton_Click(sender As Object, e As EventArgs) Handles SearchGoButton.Click

        Dim temp As Integer = 0
        For i As Integer = 0 To frPlanMain.dGVPlan.RowCount - 1
            For j As Integer = 0 To frPlanMain.dGVPlan.ColumnCount - 1
                If frPlanMain.dGVPlan.Rows(i).Cells(j).Value.ToString = SearchTextBox.Text Then
                    MsgBox("Item found")
                    temp = 1
                End If
            Next
        Next
        If temp = 0 Then
            MsgBox("Item not found")
        End If

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

在SearchWindow中

 <table>
        <tr><td style="padding:2px">Traiff</td><td style="padding:8px"><a data-tel="974-4433665588" href="tel:974-4433665588" data-rel="external" style="color:#333946 !important">974-4433665588</a></td></tr>
       	<tr><td style="padding:2px">Divil</td><td style="padding:8px"><a data-tel="974-4433665588" style="color:#333946 !important">974-4433665588</a></td></tr>
       	</table>

在frPlanMain

     Public Property dGVPlan() As DataGridView
           Get
             Return _dGVPlan
           End Get
           Set
            _dGVPlan = Value
           End Set
     End Property
     Private _dGVPlan As DataGridView

在SearchWindow中使用该网格变量_dGVPlan

     cn.Close()
     dGVPlan.DataSource = dt
     Dim f As New SearchWindow()
     f.dGVPlan = frPlanMain.dGVPlan
     SearchWindow.Show()