将Gridview列编辑为MySQL

时间:2016-04-08 10:45:36

标签: mysql vb.net datagridview

当我尝试将代码更新public override ObservableCollection<object> Collection { get; set; } 以更新到我的数据库时,我收到以下错误消息

  

对象引用未设置为对象的实例

但我无法弄明白。非常感谢我能得到的任何帮助。

GridViewBox

enter image description here

1 个答案:

答案 0 :(得分:0)

希望您点击Update button而不选择DataGrid

中的任何行

更改Update按钮Click事件,如下所示:

 Private Sub ButtonUpdate_Click(sender As Object, e As EventArgs) Handles ButtonUpdate.Click
     If DataGridView1.SelectedRows.Count > 0 Then
        Dim id As String = DataGridView1.SelectedRows(0).Cells(0).Value
        UpdateDG(id)
     End If
 End Sub

<强>更新

请声明并初始化您的Sql Adapter

使用如下:

SqlDataAdapter adapter = new SqlDataAdapter();

    //UPDATE DATABASE
    Private Sub UpdateDG(id As String)
        Dim sql As String = "UPDATE people SET name='" + TextBox_name.Text + "', position='" + TextBox_position.Text + "', team='" + TextBox_team.Text + "' WHERE ID='" + id + "'"

        //Open CON, EXECUTE UPDATE, CLOSE

        Try
            SqlDataAdapter adapter = new SqlDataAdapter();
            con.Open()
            adapter.UpdateCommand = con.CreateCommand()
            adapter.UpdateCommand.CommandText = sql
            If adapter.UpdateCommand.ExecuteNonQuery() > 0 Then
                MsgBox("You have now updated")
                cleartxt()
            End If
            con.Close()
            //REFRESH
            hent()
        Catch ex As Exception
            MsgBox(ex.Message)
            con.Close()
        End Try
    End Sub