Jump to content

inserir linha DataGridView


dianaemanuel
 Share

Recommended Posts

Boas tardes,

Tenho um problema quando estou a inserir novas linhas numa DataGridView.

O problema é que quando faço o Me.Validate() a linha da grid perde os dados.

Isto só acontece quando tou enter na linha, porque se fizer TAB a linha fica na grid.

Edited by dianaemanuel
Link to comment
Share on other sites

Código para criar novo registro de dados

 Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
 Dim conexao As New OleDb.OleDbConnection
    Dim sqlCadTelGravar As String
    Dim Cm As New OleDb.OleDbCommand(sqlCadTelGravar, conexao)
    Try
	    conexao.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\root\Documents\usuariosbd1.accdb;Jet OLEDB:Database Password= ")
	    conexao.Open()
	    sqlCadTelGravar = "INSERT INTO tbldata (Nome,Senha) VALUES ( ?,?)"
	    Cm.CommandText = sqlCadTelGravar
	    Cm.Parameters.Add("@nome", OleDb.OleDbType.VarChar).Value = TextBox1.Text
	    Cm.Parameters.Add("@senha", OleDb.OleDbType.VarChar).Value = TextBox2.Text
	    Cm.Connection = conexao
	    Dim vCadTelgravar As Integer = Cm.ExecuteNonQuery()
	    If vCadTelgravar < 1 Then
		    MsgBox("O registo não foi efectuado")
	    Else
		    MsgBox("Registo efectuado com sucesso")
	    End If

    Catch ex As Exception
	    MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
    conexao.Close()

Ajude-me?

Link to comment
Share on other sites

O código é o seguinte:


Delegate Sub GravarDadosDataGridView(ByVal i As Integer)

Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    Try

		    If Me.DataGridView1.CurrentCell.ColumnIndex <> Me.DataGridView1.Columns.Count - 1 Then
			    Dim method As New GravarDadosDataGridView(AddressOf GravarDados)
			    DataGridView1.BeginInvoke(method, e.RowIndex)
		    End If

    Catch ex As Exception
	    MessageBox.Show(ex.Message, "DEFIR", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
   End Sub

Private Sub GravarDados(ByVal rowIndex As Integer)
    If Me.Validate() Then
	    VendasBindingSource.EndEdit()
	    Me.VendasTableAdapter.Update(CType(DataGridView1.DataSource, DataTable))
    End If
    Me.BeginInvoke(New MethodInvoker(AddressOf Refresh))
   End Sub

Private Sub Refresh()
Me.VendasTableAdapter.Fill(Me.DataSet1.Vendas)
DataGridView1.DataSource = Me.DataSet1.Vendas
End SUb
Link to comment
Share on other sites

Com este codigo das o update a base de dados

Private cmd As OracleCommand

Private da As OracleDataAdapter

Private cb As OracleCommandBuilder

Private ds As DataSet

da = New OracleDataAdapter(cmd)

cb = New OracleCommandBuilder(da)

ds = New DataSet()

da.Fill(ds)

"nome da grid".DataSource = ds.Tables(0)

da.Update(ds.Tables(0))

Edited by killerbest
Link to comment
Share on other sites

Mas o problema é que a linha da grid perde-me os dados se eu der ENTER, mas se eu der TAB funciona bem e insere a linha perfeitamente.

Eu acho que o problema não é o código para inserir ou gravar as linhas.

onde é que te da o erro mesmo?

Pois não dá erro, simplesmente se eu der ENTER não insere a linha, mas com o TAB funciona bem,.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.