加载事件的DataGridView没有填充

时间:2014-01-01 10:41:52

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

作为一个相当新的VB.NET用户,我很困惑为什么当我在form_load上调用sub时DataGridView没有填充。我一直在玩这个和google搜索一段时间,但仍然没有快乐。我知道这是我的错误,还没有足够的调试知道到底要找什么。我在form_load事件的frmMain中使用' 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

frmMain加载代码

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As EventArgs) Handles MyBase.Load


        'TODO: This line of code loads data into the 'StorageDataSet1.Customers' table. You can move, or remove it, as needed.
        Me.CustomersTableAdapter.Fill(Me.StorageDataSet1.Customers)

        'TODO: This line of code loads data into the 'StorageDataSet.User' table. You can move, or remove it, as needed.
        Me.UserTableAdapter.Fill(Me.StorageDataSet.User)


        lblDate.Text = CStr(Now)

        Timer1.Start()

        rdoCustomer.Enabled = False
        rdoCustomer.Checked = True
        rdoDepartment.Enabled = False
        rdoDepartment.Checked = False

        For Each ctrl In pnlContainer.Controls
            If TypeOf ctrl Is Button Then
                AddHandler CType(ctrl, Button).MouseDown, AddressOf btn_MouseDown
            End If
        Next

        DGVmod.fillPostings()


    End Sub

1 个答案:

答案 0 :(得分:1)

fillpostings功能上:

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