Vb.net DataGridView加载事件未填充

Vb.net DataGridView加载事件未填充,vb.net,visual-studio-2010,visual-studio,datagridview,Vb.net,Visual Studio 2010,Visual Studio,Datagridview,作为VB.NET的一个新用户,我不明白为什么在form_load上调用sub时DataGridView没有填充。我已经在这个问题上挣扎了一段时间,在谷歌上搜索,但仍然没有什么乐趣。我知道这是我的错误,但调试时还不足以确切知道要查找什么。在frmMain on form_load事件中,我使用“DGVmod.fillPostings()”调用sub。代码在一个模块中。这有区别吗?有人能告诉我我的基本错误吗。非常感谢 Imports System Imports System.Data Import

作为VB.NET的一个新用户,我不明白为什么在form_load上调用sub时DataGridView没有填充。我已经在这个问题上挣扎了一段时间,在谷歌上搜索,但仍然没有什么乐趣。我知道这是我的错误,但调试时还不足以确切知道要查找什么。在frmMain on form_load事件中,我使用“DGVmod.fillPostings()”调用sub。代码在一个模块中。这有区别吗?有人能告诉我我的基本错误吗。非常感谢

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

Module DGVmod

    Private da As OleDbDataAdapter
    Private ds As DataSet
    Private 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

        fillPostings()
        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

        fillPostings()
        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)
        fillPostings()
        loadpages()

    End Sub

    Private Sub DisplayPageInfo()

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

    End Sub

    Sub fillgrid()

        Try
            'Set the start and max records. 
            pageSize = CInt(frmMain.cmbPageSize.Text)
            maxRec = frmMain.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.

            fillPostings()
            loadpages()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        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

        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

        frmMain.DGV.DataSource = dtTemp
        DisplayPageInfo()
        'fillPostings()
    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

        fillPostings()
        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(frmMain.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.

        fillPostings()
        loadpages()

    End Sub
End Module

fillpostings
功能中:

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

请附上您的
表格的所有相关代码。
。b约恩,我已经发布了我在mod文件中使用的所有代码。在frmMain load事件中,我只是调用sub。您也指的是什么代码。谢谢这就是你想要的Bjorn?是的,但是你能不能也添加一部分来填充
DataGridView
?对不起,我不明白。我正在调用“DGVmod.fillPostings()”进行填充。我是否也必须调用fillgrid子?Al,数据网格“DGV”在frmMain中,而不是mod中。我如何从国防部推荐它。ThanksI尝试这样做:frmMain.DGV.DataSource=dtSource,但它仍然没有填充到frmMain加载事件中。将
公共剪切数据源定义为DatTable
。并调用fill posting函数进行填充,然后将dtSource分配给DGV。
    ' Set the source table.
    dtSource = ds.Tables("Postings")
    Me.DataGridView1.DataSource = dtSource