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

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

本文介绍了如何将值从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

推荐答案

这篇关于如何将值从Datagridview保存到数据库中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 03:46