如何使用datagridview

如何使用datagridview

本文介绍了如何使用datagridview(vb.net)更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

这是我的代码,但是反过来,它在数据库更新时将更改提交到datagridview.

Here is my code, but its the other way around, it commits changes to the datagridview when the database is updated.

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms

Public Class Form1
Inherits System.Windows.Forms.Form

    Private bindingSource1 As New BindingSource()
    Private dataAdapter As New OleDbDataAdapter()

    <STAThreadAttribute()> _
    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\GRN.mdb;User Id=admin;Password=;"
        Dim selectCommand As String
        Dim connection As New OleDbConnection(connectionString)

        selectCommand = "select * from GRNITEMS ORDER BY GRNNO, ITEMCODE"
        Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

        With DataGridView1
            .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
        End With

        Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
        Dim table As New DataTable()

        table.Locale = System.Globalization.CultureInfo.InvariantCulture

        Me.dataAdapter.Fill(table)
        Me.bindingSource1.DataSource = table

        Dim data As New DataSet()

        data.Locale = System.Globalization.CultureInfo.InvariantCulture

        DataGridView1.DataSource = Me.bindingSource1
        Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
        Me.DataGridView1.AutoResizeColumns( _
            DataGridViewAutoSizeColumnsMode.AllCells)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim table As New DataTable()

        Me.bindingSource1 = Me.DataGridView1.DataSource
        table = Me.bindingSource1.DataSource

        Me.dataAdapter.Update(table)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

End Class

推荐答案

此行:

Me.dataAdapter.Update(table)

使用dataAdapter使用数据库中的最新数据更新表.

updates your table with recent data from your database using your dataAdapter.

这就是问题所在.我对VB.NET不够熟悉,无法为您编写代码以使用gridview中的新数据更新数据库.

That's where the problem is. I'm not familiar enough with VB.NET to write you the code to update your database with your new data from gridview.

这篇关于如何使用datagridview(vb.net)更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:36