如何添加datagridview记录 - 关闭

时间:2015-06-03 04:26:07

标签: vb.net visual-studio-2010 datagridview

当我尝试保存记录时,在关闭表单并再次运行之前,它不会显示在datagridview中

  Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

            provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
            'Change the following to your access database location
            dataFile = "C:\Users\DELL\Downloads\WindowsApplication1\WindowsApplication1\Database1.accdb"
            connString = provider & dataFile
            myConnection.ConnectionString = connString
            myConnection.Open()
            Dim str As String
            str = "insert into tblODR ([ID], [Office], [Elements], [Objectives], [Initiatves]) values (?, ?, ?, ?, ?)"
            Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
            cmd.Parameters.Add(New OleDbParameter("UPC", CType(IDTextBox.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("Item", CType(OfficeTextBox.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("Cost", CType(ElementsTextBox.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("Price", CType(ObjectivesTextBox.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("Quantity", CType(InitiatvesTextBox.Text, String)))
            Try
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                myConnection.Close()
                IDTextBox.Clear()
                OfficeTextBox.Clear()
                ElementsTextBox.Clear()
                ObjectivesTextBox.Clear()
                InitiatvesTextBox.Clear()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try


        End Sub

以下代码用于删除目的,我需要像这样的代码来添加/保存记录。

 Try
                Dim int As Integer = DataGridView1.SelectedRows(0).Index

                DataGridView1.Rows.Remove(DataGridView1.SelectedRows(0))
                ds.Tables(0).Rows(int).Delete()
                da.Update(ds, "datainfo")
                MessageBox.Show("Success!")

            Catch ex As Exception

            End Try

1 个答案:

答案 0 :(得分:0)

我已经解决了它,我已经添加了一些代码..并且在bindingNavigator中,我必须在每次添加记录时点击加号(+)。虚拟:3 ...对不起是一个新手

protected void onDraw(Canvas canvas){
    for(Path p : paths){
        canvas.drawPath(p, drawPaint);
    }
    canvas.drawPath(drawPath, drawPaint);
}

//Here all the code for the Motion Event. I will show only the Action Up.

case MotionEvent.ACTION_UP:
        drawPath.addCircle(x, y, radius, Path.Direction.CCW); //Example of drawing object
        paths.add(drawPath);
        drawPath = new Path();
    break;