Visual Basic 2010,数据库 - 如何填写特定字段

时间:2016-04-19 22:15:07

标签: vb.net datagridview ms-access-2010 database-connection

我在Visual Basic中创建数据库连接。当用户点击“借书”时,它将所选数据传输到用于借出书籍的表格中。一切正常,但我需要最后一件事。在借给表的数据库中,我有“借出结束日期”的属性,我希望每当用户借用今天日期+ 1个月的书时填写它(例如,如果现在是2016年4月19日,结束日期将是19 / 05/2016)。我为此创建了方法“BorrowTime”,但我不知道应该输入什么,如何将插入的行的值输入“row”。对于代码和形式的可怕外观感到抱歉......

Public Class frm_UserView
    Dim BooksSource As New BindingSource
    Dim BorrowSource As New BindingSource

    Dim BooksView As New DataView
    Dim BorrowView As New DataView

    Dim ds As New DataSet

    Dim Borrow As Byte


    Private Sub frm_UserView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        tmr_Timer.Start()
        Me.LoanerBooksTableAdapter.Fill(Me.GMITLibraryDataSet1.LoanerBooks)
        Me.BooksTableAdapter.Fill(Me.GMITLibraryDataSet.Books)

        BooksSource = dgd_UserBookView.DataSource
        BooksView = CType(BooksSource.List, DataView)

        ds = BooksView.DataViewManager.DataSet.Clone

        BorrowSource.DataSource = ds
        BorrowSource.DataMember = "Books"

        BorrowView = CType(BorrowSource.List, DataView)
        dgd_User_Borrow_View.DataSource = BorrowSource



    End Sub

    Private Sub btn_Borrow_Click(sender As System.Object, e As System.EventArgs) Handles btn_BorrowBook.Click
        Borrow = 1
        MoveBooks(dgd_UserBookView, BorrowView, Borrow)

        dgd_User_Borrow_View.Sort(dgd_User_Borrow_View.Columns(0), System.ComponentModel.ListSortDirection.Ascending)

        BorrowSource.MoveLast()
        BorrowSource.MoveLast()
    End Sub
    Private Sub MoveBooks(ByRef source As DataGridView, ByRef target As DataView, ByRef borrow As Byte)
        For i = 0 To source.SelectedRows.Count - 1
            Dim numberOfRow As Integer
            Dim row As DataRowView
            row = target.AddNew()
            Dim col As Int16 = 0
            For Each cell As DataGridViewCell In source.SelectedRows(i).Cells
                row.Item(col) = cell.Value
                If borrow = 1 Then
                    numberOfRow = 0
                    BorrowTime(numberOfRow, dgd_User_Borrow_View)
                End If
                col = col + 1
            Next
        Next

        Dim count As Int16 = source.SelectedRows.Count

        For i = 0 To count - 1
            source.Rows.RemoveAt(source.SelectedRows(0).Index)
        Next
    End Sub
    Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
        'Code for adding date
    End Sub

    Private Sub btn_ReturnBook_Click(sender As System.Object, e As System.EventArgs) Handles btn_ReturnBook.Click
        Borrow = 0
        MoveBooks(dgd_User_Borrow_View, BooksView, Borrow)

        dgd_UserBookView.Sort(dgd_UserBookView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)

        BooksSource.MoveLast()
        BooksSource.MoveLast()
    End Sub

    Private Sub btn_UserLogOut_Click(sender As System.Object, e As System.EventArgs) Handles btn_UserLogOut.Click
        frm_Login.Show()
        Me.Hide()
    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles tmr_Timer.Tick
        Dim countBooks As Integer
        Dim countLent As Integer
        countBooks = BooksSource.Count
        countLent = BorrowSource.Count

        lbl_BookCounter.Text = "There are " + countBooks.ToString + " books"
        lbl_BorrowCounter.Text = "You have lent " + countLent.ToString + " books"
    End Sub
End Class

我需要在此处输入代码:

 Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
       'Code for adding date
 End Sub

我的表格

enter image description here

1 个答案:

答案 0 :(得分:0)

公共类frm_UserView     Dim BooksSource作为新的BindingSource     Dim BorrowSource作为新的BindingSource

Dim BooksView As New DataView
Dim BorrowView As New DataView

Dim ds As New DataSet

Dim Borrow As Byte


Private Sub frm_UserView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    tmr_Timer.Start()
    Me.LoanerBooksTableAdapter.Fill(Me.GMITLibraryDataSet1.LoanerBooks)
    Me.BooksTableAdapter.Fill(Me.GMITLibraryDataSet.Books)

    BooksSource = dgd_UserBookView.DataSource
    BooksView = CType(BooksSource.List, DataView)

    ds = BooksView.DataViewManager.DataSet.Clone

    BorrowSource.DataSource = ds
    BorrowSource.DataMember = "Books"

    BorrowView = CType(BorrowSource.List, DataView)
    dgd_User_Borrow_View.DataSource = BorrowSource



End Sub

Private Sub btn_Borrow_Click(sender As System.Object, e As System.EventArgs) Handles btn_BorrowBook.Click
    Borrow = 1
    MoveBooks(dgd_UserBookView, BorrowView, Borrow)

    dgd_User_Borrow_View.Sort(dgd_User_Borrow_View.Columns(0), System.ComponentModel.ListSortDirection.Ascending)

    BorrowSource.MoveLast()
    BorrowSource.MoveLast()
End Sub
Private Sub MoveBooks(ByRef source As DataGridView, ByRef target As DataView, ByRef borrow As Byte)
    For i = 0 To source.SelectedRows.Count - 1
        Dim numberOfRow As Integer
        Dim row As DataRowView
        row = target.AddNew()
        Dim col As Int16 = 0
        For Each cell As DataGridViewCell In source.SelectedRows(i).Cells
            row.Item(col) = cell.Value
            If borrow = 1 Then
                numberOfRow = 0
                BorrowTime(numberOfRow, dgd_User_Borrow_View)
            End If
            col = col + 1
        Next
    Next

    Dim count As Int16 = source.SelectedRows.Count

    For i = 0 To count - 1
        source.Rows.RemoveAt(source.SelectedRows(0).Index)
    Next
End Sub
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
    'Code for adding date
End Sub

Private Sub btn_ReturnBook_Click(sender As System.Object, e As System.EventArgs) Handles btn_ReturnBook.Click
    Borrow = 0
    MoveBooks(dgd_User_Borrow_View, BooksView, Borrow)

    dgd_UserBookView.Sort(dgd_UserBookView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)

    BooksSource.MoveLast()
    BooksSource.MoveLast()
End Sub

Private Sub btn_UserLogOut_Click(sender As System.Object, e As System.EventArgs) Handles btn_UserLogOut.Click
    frm_Login.Show()
    Me.Hide()
End Sub

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles tmr_Timer.Tick
    Dim countBooks As Integer
    Dim countLent As Integer
    countBooks = BooksSource.Count
    countLent = BorrowSource.Count

    lbl_BookCounter.Text = "There are " + countBooks.ToString + " books"
    lbl_BorrowCounter.Text = "You have lent " + countLent.ToString + " books"
End Sub

结束班