我在Visual Studio 2010中使用VB
我有三个表单(MainForm,SaveForm和updateform),其中saveForm和updateform具有相同的表单设计,在Mainform中有1个datagridview,两个按钮(btnSave以显示saveForm和btnUpdate以显示updateform)
可以更新datagridview中选定行上的数据条件是该行上的所有数据都出现在updateform中?
我有这个编码
这是更新形式
Dim idsend As String
Conn = New OleDb.OleDbConnection(Konek())
Conn.Open()
Comm = New OleDb.OleDbCommand
Comm.Connection = Conn
Comm.CommandText = "SELECT * FROM TabelSiswa WHERE No =" & idsend
Reader = Comm.ExecuteReader()
Reader.Read()
txtName.Text = reader.getvalue(1)
这是在Mainform上
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FormUpdate.idSend = dgv.CurrentRow.Cells(0).Value
@Vland解决方案可以工作,
但我认为更好的做法是将变量保留idsend
为Private,因为它仅在类外部使用一次(传递给类实例)
Private _idsend As Int32
然后将其值传递给构造函数
Public Sub New(inIdSend As Int32)
_idsend = inIdSend
End Sub
现在,您可以在 FormUpdate
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim id as Int32 = dgv.CurrentRow.Cells(0).Value
Dim update As New FormUpdate(id)
update.ShowDialog()
End Sub
或者,如果您不想再创建一次与数据库的连接,请使用datagridview中的所需数据作为参数来创建一个构造函数...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句