代码更新后DataGridView抛出空引用错误

时间:2013-12-31 17:59:11

标签: vb.net visual-studio-2010 visual-studio datagridview

作为VB.NET的一个相当新的用户,我认为在学习了DataGridView的基础知识和其他基​​本知识后,我试图掌握ListView。但是,我有一个错误“对象引用未设置为对象的实例”。让我试着解释一下我认为我是如何创建这个错误的,但似乎找不到出路。

最初,我在1 winform中有代码, DGVtest.vb ,按下'填充网格'按钮后,它填充了网格空值和所有内容。我认为我要做的是保持代码清洁,从长远来看,有更多的控制权,就是将代码移出 DGVtest.vb 并创建一个模块, DGVmod.vb < / strong>并将代码放在那里。然而,作为一个新用户,我现在发现自己完全脱离了我的深度,并寻求有关如何继续前进的帮助。

因此,要概述,DGV位于 DGVtest.vb ,并带有一个“填充网格”按钮,该按钮调用“ DGVmod.fillgrid()”来填充网格。如果有人能提供任何帮助,我将非常感激。非常感谢

Imports System
Imports System.Data
Imports System.Data.OleDb

Module DGVmod

    Private da As OleDbDataAdapter
    Private ds As DataSet
    Public dtSource As DataTable

    Private PageCount As Integer
    Private maxRec As Integer
    Private pageSize As Integer
    Private currentPage As Integer
    Private recNo As Integer

    Sub fillPostings()

        Dim conn As OleDbConnection = New OleDbConnection(My.Settings.storageConnectionString)

        'Set the DataAdapter's query.
        da = New OleDbDataAdapter("select * from Postings", conn)
        ds = New DataSet()

        ' Fill the DataSet.
        da.FillSchema(ds, SchemaType.Source, "Postings")
        da.Fill(ds, "Postings")

        ' Set the source table.
        dtSource = ds.Tables("Postings")

    End Sub

    Sub btnprevious()

        If Not CheckFillButton() Then Return

        If currentPage = PageCount Then
            recNo = pageSize * (currentPage - 2)
        End If

        currentPage = currentPage - 1

        'Check if you are already at the first page.
        If currentPage < 1 Then
            MessageBox.Show("You are at the First Page!")
            currentPage = 1
            Return
        Else
            recNo = pageSize * (currentPage - 1)
        End If

        loadpages()

    End Sub

    Sub btnnext()

        'If the user did not click the "Fill Grid" button then Return
        If Not CheckFillButton() Then Return

        'Check if the user clicked the "Fill Grid" button.
        If pageSize = 0 Then
            MessageBox.Show("Set the Page Size, and then click the ""Fill Grid"" button!")
            Return
        End If

        currentPage = currentPage + 1

        If currentPage > PageCount Then
            currentPage = PageCount

            'Check if you are already at the last page.
            If recNo = maxRec Then
                MessageBox.Show("You are at the Last Page!")
                Return
            End If
        End If

        loadpages()

    End Sub

    Sub btnlast()

        If Not CheckFillButton() Then Return

        ' Check if you are already at the last page.
        If recNo = maxRec Then
            MessageBox.Show("You are at the Last Page!")
            Return
        End If

        currentPage = PageCount

        recNo = pageSize * (currentPage - 1)

        loadpages()

    End Sub

    Private Sub DisplayPageInfo()

        DGVtest.txtDisplayPageNo.Text = "Page " & currentPage.ToString & "/ " & PageCount.ToString

    End Sub

    Sub fillgrid()

        Try
            'Set the start and max records. 
            pageSize = CInt(DGVtest.cmbPageSize.Text)
            maxRec = DGVtest.DGV.Rows.Count
            PageCount = maxRec \ pageSize

            ' Adjust the page number if the last page contains a partial page.
            If (maxRec Mod pageSize) > 0 Then
                PageCount = PageCount + 1
            End If

            'Initial seeings
            currentPage = 1
            recNo = 0

            ' Display the content of the current page.
            loadpages()

        Catch ex As Exception
            MessageBox.Show(ex.InnerException.ToString)
        End Try

    End Sub

    Private Sub loadpages()

        Dim i As Integer
        Dim startRec As Integer
        Dim endRec As Integer
        Dim dtTemp As DataTable
        'Dim dr As DataRow

        'Duplicate or clone the source table to create the temporary table.

        dtTemp = dtSource.Clone <--- ERROR IS HERE

        If currentPage = PageCount Then
            endRec = maxRec
        Else
            endRec = pageSize * currentPage
        End If

        startRec = recNo

        'Copy the rows from the source table to fill the temporary table.
        For i = startRec To endRec - 1
            dtTemp.ImportRow(dtSource.Rows(i))
            recNo = recNo + 1
        Next

        DGVtest.DGV.DataSource = dtTemp
        DisplayPageInfo()

    End Sub

    Sub btnfirst()

        If Not CheckFillButton() Then Return

        ' Check if you are already at the first page.
        If currentPage = 1 Then
            MessageBox.Show("You are at the First Page!")
            Return
        End If

        currentPage = 1
        recNo = 0

        loadpages()

    End Sub

    Private Function CheckFillButton() As Boolean

        'Check if the user clicks the "Fill Grid" button.
        If pageSize = 0 Then
            MessageBox.Show("Set the Page Size, and then click the ""Fill Grid"" button!")
            CheckFillButton = False
        Else
            CheckFillButton = True
        End If
    End Function

    Sub cmbpage()

        'Set the start and max records. 
        pageSize = CInt(DGVtest.cmbPageSize.Text)
        maxRec = dtSource.Rows.Count
        PageCount = maxRec \ pageSize

        ' Adjust the page number if the last page contains a partial page.
        If (maxRec Mod pageSize) > 0 Then
            PageCount = PageCount + 1
        End If

        'Initial seeings
        currentPage = 1
        recNo = 0

        ' Display the content of the current page.
        loadpages()

    End Sub
End Module

0 个答案:

没有答案