如何将值从Datagridview保存到数据库中的表

问题描述:

Imports DemoExcelFileHandling.DataSet1
Imports DemoExcelFileHandling.DataSet1TableAdapters


Public Class ReadFromExcel
    Private dt As New dtMonthDataTable
    Private ta As New taMonth
    Private bs As New BindingSource

    Private Sub BindUI()
        dgvExcelData.AutoGenerateColumns = False
        colYear.DataPropertyName = "Year1"
        colMonth.DataPropertyName = "Month1"
        colFacility.DataPropertyName = "Facility"
        colProductCode.DataPropertyName = "ProductCode"
        colProductDesc.DataPropertyName = "ProductDesc"
        colUnit.DataPropertyName = "UnitOfIssue"
        colBegBalance.DataPropertyName = "Beginning_Balance"
        colQtyReceived.DataPropertyName = "QuantityReceived"
        colQtyDispensed.DataPropertyName = "QuantityDispensed"
        colLosses.DataPropertyName = "Losses  "
        colAdjustments.DataPropertyName = "Adjusments"
        colEndingBal.DataPropertyName = "Ending_Balance_Book"
        colEndingBalPhysical.DataPropertyName = "Ending_Balance_Physical"
        ColQtyLeft.DataPropertyName = "3_months_shelflife"
        colQtyRequired.DataPropertyName = "Quantity_Ordered"



    End Sub

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        If ofdOpenFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtFileName.Text = ofdOpenFile.FileName
        End If
    End Sub

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        If Not String.IsNullOrEmpty(txtFileName.Text) Then
            Try
                btnClose.Enabled = False
                Dim OExcelHandler As New ExcelHandler()
                Dim ds As DataSet = OExcelHandler.GetDataFromExcel(txtFileName.Text.Trim())

                If ds IsNot Nothing Then
                    dgvExcelData.SelectionMode = DataGridViewSelectionMode.FullRowSelect
                    dgvExcelData.EditMode = DataGridViewEditMode.EditProgrammatically
                    dgvExcelData.DataSource = ds.Tables(0)
                End If

            Catch ex As Exception

            Finally
                btnClose.Enabled = True
            End Try
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        bs.EndEdit()
        ta.Update(dt)
    End Sub

    Private Sub ReadFromExcel_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BindUI()
    End Sub
End Class