使用带有datagridview

时间:2016-02-21 11:58:33

标签: mysql vb.net datagridview

大家好我需要帮助更新datagridview上的数据。方案是当我单击“搜索”时,它将查询相应的数据并显示我想要的数据网格视图。然后我想更新如果他们的任何更改或从datagridview删除的数据然后也更新到我的mysql表的新数据或记录每当我点击更新按钮,它会提示消息框,如果他们是成功更改我的数据,并提示是否他们的数据没有变化。我真的需要帮助代码更新按钮。 这是我对datagridview的搜索按钮的代码:

Imports MySql.Data.MySqlClient
Public Class Form1
    Dim conn As New MySqlConnection
    Dim result As Integer
    Dim cmd As New MySqlCommand
    Dim da As New MySqlDataAdapter
    Dim ds As New DataSet
    Dim dr As MySqlDataReader
    Dim sql As String
    Dim dt As New DataTable

 Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        con.ConnectionString = ("server=127.0.0.1;user id=root;password=12345;database=dbsis3bkenth;")

Try
            conn.Open()

        sql = "SELECT LName,FName,MI FROM tblsisterbrother where IDNoBrodSis = '" & cbIDNo.Text & "'"
        cmd = New MySqlCommand(sql, conn)
        dr = cmd.ExecuteReader
        dr.Read()

        If dr.HasRows = True Then
            MessageBox.Show("Record Found.!")
        Else
            MessageBox.Show("Record Unfound.!")
        End If
        dr.Close()
    Catch ex As MySqlException
        MessageBox.Show("Error in searching to database:error is:" & ex.Message)
        Exit Sub
    End Try
    dr.Close()
    RemoveHandler DataGridView1.CellValidating, AddressOf DataGridView1_CellValidating
    Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter
    DataAdapter1.SelectCommand = cmd
    DataAdapter1.Fill(ds, "tblsisterbrother")
    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "tblsisterbrother"
    conn.Dispose()
    conn.Close()

End Sub

然后我想要一个更新代码,它将放在这里:

 Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click

如果受影响的行,我希望出现这种消息框,此代码不会运行到我想要的内容,仅供参考,如何更新。请帮帮我:)。

If DataGridView1.DataSource.GetChanges() Is Nothing Then
            MessageBox.Show("The table contains no changes to save.")
        Else
            Dim rowsAffected As Integer = da.Update(dt)
            If rowsAffected = 0 Then
                MessageBox.Show("No rows were affected by the save operation.")
            Else
                MessageBox.Show(rowsAffected & " rows were affected by the save operation.")
            End If
        End If
        End Sub

1 个答案:

答案 0 :(得分:0)

问题在于,当您从更新数据库时要使用的网格填充网格时,使用不同的DataAdapter。

您可以简单地更改填充网格的代码并使用在类中声明的全局dataadapter

' Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter'
da.SelectCommand = cmd
da.Fill(ds, "tblsisterbrother")
dataGridView1.DataSource = ds.Tables(0)
.....

现在当您调用Update方法时,您正在使用相同的适配器,只需使用MySqlCommandBuilder为其构建所需的update / insert和delete命令提供帮助

DataGridView1.EndEdit()
Dim dt As DataTable
dt = TryCast(DataGridView1.DataSource, DataTable)
If dt.GetChanges() Is Nothing Then
    MessageBox.Show("The table contains no changes to save.")
Else
    Dim builder = new MySqlCommandBuilder(da)
    Dim rowsAffected As Integer = da.Update(dt)
    If rowsAffected = 0 Then
        MessageBox.Show("No rows were affected by the save operation.")
    Else
        MessageBox.Show(rowsAffected & " rows were affected by the save operation.")
    End If
End If