datagridview插入,更新 [英] datagridview insert, update
问题描述
我正在做一个vb.net 2010项目,我很难创建更新并直接插入到datagridview中而不使用按钮,这一事实应该通过存储过程来完成,我使用连接字符串连接sql server数据库.
-更新-
正在输入,只需按Tab并按Enter即可保存,我想在键入结束时按Enter键输入
I''m doing a project vb.net 2010 and I''m having difficulty creating a update and insert directly into the datagridview without using buttons, this fact should be made through stored procedure, I''m using conection string to connect the sql server database.
-- Update --
are entering only have to press a Tab and Enter to save, I would like to press the enter key at the end of typing to enter
Private Sub DadoUnico_KeyDown (ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DadoUnico.KeyDown
If e.KeyCode = Keys.Enter Then
SendKeys.Send ("{ENTER}")
DadoUnico.Enabled = True
try
cn.Open ()
cd = New SqlCommand ("sp_I_Cad_Unioinsere", cn)
cd.CommandType = Data.CommandType.StoredProcedure
cd.Parameters.Add ("@ idlog" SqlDbType.VarChar). Value = frmCad_Cadastro.Codigo.Text
cd.Parameters.AddWithValue ("@ bikelog" frmCad_Cadastro.DadoUnico.CurrentRow.Cells ("Bike_Log"). Value)
cd.Parameters.AddWithValue ("@ ModeloLog" frmCad_Cadastro.DadoUnico.CurrentRow.Cells ("Modelo_Log"). Value)
cd.Parameters.AddWithValue ("@ CorLog" frmCad_Cadastro.DadoUnico.CurrentRow.Cells ("Cor_Log"). Value)
cd.Parameters.AddWithValue ("@ NumSerieLog" frmCad_Cadastro.DadoUnico.CurrentRow.Cells ("NumSerie_Log"). Value)
cd.Parameters.AddWithValue ("@ AjuSisRetLog" frmCad_Cadastro.DadoUnico.CurrentRow.Cells ("AjuSisRet_Log"). Value)
cd.Parameters.AddWithValue ("@ OleoPrefLog" frmCad_Cadastro.DadoUnico.CurrentRow.Cells ("OleoPref_Log"). Value)
cd.Parameters.AddWithValue ("@ PresPneuLog" frmCad_Cadastro.DadoUnico.CurrentRow.Cells ("PresPneu_Log"). Value)
cd.Parameters.AddWithValue ("@ AltFreiosLog" frmCad_Cadastro.DadoUnico.CurrentRow.Cells ("AltFreios_Log"). Value)
cd.Parameters.AddWithValue ("@ PresAmorLog" frmCad_Cadastro.DadoUnico.CurrentRow.Cells ("PresAmor_Log"). Value)
cd.Parameters.AddWithValue ("@ PresSuspLog" frmCad_Cadastro.DadoUnico.CurrentRow.Cells ("PresSusp_Log"). Value)
cd.ExecuteNonQuery ()
MsgBox ("successfully inserted")
Catch ex As Exception
MsgBox ("Error:" & Err.Description)
end Try
cn.Close ()
end If
没有给出错误,此插入插件希望对其进行改进.
该过程是正确的,为什么通过输入仅需按Tab键并输入即可进入.我只会在用户"类型中输入precionar最后一个单元格来添加.
和我一样工作.想要一种可以在按下Enter键时填充要保存的单元格的方法是另一个可以在适当位置使用此方法的事件-KeyDown. RowLeave告诉了我这一点,但是因为他们给连接打开了而不能.
这段代码很好用,但是不能满足我的需求.
但已经设法用其他方式解决了我的问题.
感谢您的关注.
is not giving error, this inserting the insert would like to improve it.
the procedure is correct, why does this by entering only have to press the tab key and enter, to enter. I would only enter precionar last cell in the User type to add.
worked the same as I did. wanted a method that fill the cell to be saved when you press the enter key is another event that could use this in place - KeyDown. RowLeave told me this, but could not because they gave the connection was open.
this code works good, but not for what I need.
but already managed to solve my problem with the code otherwise.
I appreciate the attention.
推荐答案
您为什么有这段代码?
Why do you have this bit of code?
SendKeys.Send ("{ENTER}")
您只是检查用户是否按下了回车键,然后基本上只再次按了它,这可能只是再次调用整个方法并使您陷入无限循环.
删除sendkeys语句,该语句应该在用户每次按Enter键时都有效.如果希望它同时适用于Enter键和Tab键,则应将If e.KeyCode
语句更改为select case语句,如下所示:
You just checked to see if the user pressed the enter key, and then you basically just press it again, which might just call that whole method again and put you in an infinite loop.
Remove the sendkeys statement and it should work any time the user presses the enter key. If you want it to work for both the enter key and the tab key, you should change your If e.KeyCode
statement into a select case statement like this:
Select Case e.KeyCode
Case Keys.Enter, Keys.Tab
'Code that calls stored procedure
End Select
此代码效果很好,但不是我需要的.
但是我设法用其他方式解决了我的问题.
This code works good, but not for what I need.
However I have managed to solve my problem with the code otherwise.
Public Class frmPrincipal
Dim strConexao As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & Environment.CurrentDirectory & "\grid.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
Dim isNovo As Boolean = False
Private Sub frmPrincipal_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
grid1.Columns.Add("colId", "Id")
grid1.Columns.Add("colNome", "Nome")
grid1.Columns.Add("colNumero", "Numero")
grid1.Columns(0).ReadOnly = False
preencherDados(grid1)
End Sub
Public Sub adicionar(nome As String, numero As Integer)
Dim conexao As SqlConnection = New SqlConnection(strConexao)
Dim Comando As SqlCommand = New SqlCommand("INSERT INTO Teste(Nome, Numero) Values ('" & nome & "','" & numero & "')", conexao)
Try
conexao.Open()
Comando.ExecuteNonQuery()
conexao.Close()
Catch ex As SqlException
MsgBox(ex.Message)
End Try
End Sub
Public Sub atualizar(id As Integer, nome As String, numero As Integer)
Dim conexao As SqlConnection = New SqlConnection(strConexao)
Dim Comando As SqlCommand = New SqlCommand("UPDATE Teste Set Nome = '" & nome & "', Numero = '" & numero & "' WHERE id = " & id, conexao)
Try
conexao.Open()
Comando.ExecuteNonQuery()
conexao.Close()
Catch ex As SqlException
MsgBox(ex.Message)
End Try
End Sub
Public Sub preencherDados(grid As DataGridView)
Dim conexao As SqlConnection = New SqlConnection(strConexao)
Dim comando As SqlCommand = New SqlCommand("SELECT * FROM Teste", conexao)
Dim dr As SqlDataReader
Try
conexao.Open()
dr = comando.ExecuteReader
Do While dr.Read
grid1.Rows.Add(dr(0).ToString, dr(1).ToString, dr(2).ToString)
Loop
dr.Close()
conexao.Close()
Catch ex As SqlException
MsgBox(ex.Message)
End Try
End Sub
Private Sub grid1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles grid1.CellBeginEdit
If grid1.CurrentRow.IsNewRow Then
isNovo = True
End If
End Sub
Private Sub grid1_RowValidated(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grid1.RowValidated
isNovo = False
End Sub
Private Sub grid1_RowValidating(sender As Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles grid1.RowValidating
If grid1.IsCurrentRowDirty Then
grid1.CommitEdit(DataGridViewDataErrorContexts.Commit)
If isNovo = True Then
adicionar(grid1.Rows(e.RowIndex).Cells("colNome").Value, grid1.Rows(e.RowIndex).Cells("colNumero").Value)
Else
atualizar(grid1.Rows(e.RowIndex).Cells("colId").Value, grid1.Rows(e.RowIndex).Cells("colNome").Value, grid1.Rows(e.RowIndex).Cells("colNumero").Value)
End If
End If
End Sub
Public Sub removeRows(grid As DataGridView)
If grid1.Rows.Count > 0 Then
For Each dr As DataGridViewRow In grid.Rows
If Not dr.IsNewRow Then grid.Rows.Remove(dr)
Next
End If
End Sub
End Class
这篇关于datagridview插入,更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!