在Windows窗体之间传递值以更新数据库最佳实践中的记录 [英] Passing Value between Windows Forms to Update a Record in a DB Best Practice

查看:62
本文介绍了在Windows窗体之间传递值以更新数据库最佳实践中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更新数据库上的员工记录,我有2个表单来执行此操作。

第1个表单显示一个网格,其中包含一些员工的列表,用户可以选择正确的单击记录,选择添加新员工或更改或删除现有员工。如果用户选择创建新员工,我将显示第二个表单,其中包含员工主人的所有空字段。如果用户选择更改或删除员工记录,我将显示与所有填充的员工字段相同的第二个表单。

如果您想更新
a表,通常你不直接在网格上更新而是使用第二个表格

你将显示与你想要更新的记录相关的所有字段并且能够验证保存更改到数据库之前的数据。



在Form1上,我传递网格上的选定员工ID(如果有)和维护操作(创建,更改)或者删除)到之前在Form2中定义的2个属性

(如果选择了Action Create,则变量被传递为空,对于其他我传递了点击记录上的Id)。然后我在没有关闭Form1的情况下显示表格。



表格1



I need to update an employee record on a database and I have 2 forms to do that.
The 1st form shows a grid with a list of some employees, the user has the choice to right click on a record an opt for adding a new employee or changing or deleting an existing employee. If the user opts to create a new employee I am showing a 2nd form with all the empty fields for an employee master. If the user opts for changing or deleting an employee record I will show the same 2nd form with all populated employee fields.
The scenario described above is a very common when you want to update a record on
a table, normally you do not update directly on the grid instead you use a second form where
you will show all fields related to the record you want to update and be able to validate the data before saving the changes to the database.

On Form1 I am passing the Selected Employee Id on the Grid (if any) and the Maintenance Action (Create, Change or Delete) to 2 properties previously defined in Form2
(if Action Create is selected the variable is passed empty, for the others I am passing the Id on the clicked record). Then I am showing Form 2 without closing Form1.

Form 1

Sub ShowChildForm()
   form2.strEmployeeId '<== This is the Employee Id value from the Grid
   form2.strAction '<== Will contain the value "Create, Change or Delete"
   form2.Show()
End Sub



//已激活事件


// Activated event

Form1_Activated
if strAction1 = "Done"
   Refresh grid here
endif
End Sub





在表格Form2上我会更新t使用表适配器的数据库和

在Form1中定义的属性中传递给form1一个值Done,这意味着我需要刷新Grid,因为已添加,更改或删除了一条记录。如果没有完成动作,我不需要刷新网格。最后我关闭Form2,Focus回到Form1,在Form2后面打开。



表格2



On form Form2 I will update the Database using a Table Adapter and
pass back to form1 a value "Done" in a property defined in Form1 meaning that I need to refresh the Grid because a record has been added, changed or deleted. If not action was completed I don't need to refresh the grid. Finally I close Form2 and the Focus goes back to Form1 which was left open behind Form2.

Form 2

Private button_Save_Click()
   dtaTable.Update(dtsTable, myTable)
   Form1.strAction1 = "Done"
   me.close
End Sub





现在在Form1的Activated Event上我正在检查传递的值是否等于Done

所以我可以刷新网格。但是在进行调试之后,我注意到程序很多次都进入了激活事件,效率不高。我可以使用变量来控制它,因此在Activated事件上只进行一次刷新,但变量检查仍然会进行多次。有没有其他更有效的方法来实现这一点,或者在VS Basic 2012中可能是最现代的技术。

当您想要更新
上的记录时,上述场景是一种非常常见的情况。
a表,通常你不直接在网格上更新而是使用第二个表格

你将显示与你想要维护的记录相关的所有字段并能够验证数据在将更改保存到数据库之前。



提前谢谢



Now on the Activated Event of Form1 I am checking if the passed value is equal to "Done"
so I can refresh the Grid. But after doing a debug I noticed that the program goes to the Activated Event a lot of times which is not efficient. I can using a variable to control that, so the refresh is made only once on the Activated event but still the variable checking will be done many times. Is there any other more efficient way to accomplish this or maybe a most modern technique in VS Basic 2012.
The scenario described above is a very common scenario when you want to update a record on
a table, normally you do not update directly on the grid instead you use a second form where
you will show all fields related to the record you want to maintain and be able to validate the data before saving the changes to the database.

Thanks in advance

推荐答案

哦亲爱的...请,不要那样做!

相反,在Form2中创建一个表示已完成的事件,并在更新后发出信号。

Form1添加一个处理程序当它创建Form2实例并刷新网格时的事件。

没有什么需要查看变量,没有什么需要知道表格甚至不必要地存在。 (Form2不应该知道Form1甚至存在!Form1需要知道Form2,因为它创建并显示它。)



看看这个:在两个表格之间传递信息,第2部分:儿童到父母 [< a href =http://www.codeproject.com/Tips/548131/Transferring-information-between-two-forms-Part-2target =_ blanktitle =New Window> ^ ]代码是C#而不是VB,但原理是相同的,代码非常明显。
Oh dear...please, don't do it like that!
Instead, create an event in Form2 that indicates "Completed" and signal it when you have updated.
Form1 adds a handler to the event when it creates the Form2 instance and refreshes the grid then.
Nothing needs to look at variables, and nothing needs to know that forms even exist unnecessarily. (Form2 should not know that Form1 even exists! Form1 needs to know about Form2, because it cretaes it and displays it).

Have a look at this: Transferring information between two forms, Part 2: Child to Parent[^] The code is C# rather than VB, but the principle is identical, and the code is pretty obvious.


这篇关于在Windows窗体之间传递值以更新数据库最佳实践中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆