创建新记录,然后关注新记录

时间:2013-07-27 21:26:38

标签: c# database

到目前为止,我在按钮点击事件中有以下代码:

        string AccRef = Microsoft.VisualBasic.Interaction.InputBox("New Customer", "Customer Reference:", "", 0, 0);

        if (AccRef != "")
        {
            DataRow dr = dSSystem.Customers.NewRow();
            dr["AccRef"] = AccRef;
            dSSystem.Customers.Rows.Add(dr);


            frmCustomer frmCust = new frmCustomer();
            frmCust.ShowDialog();

            this.customersTableAdapter.Fill(this.dSSystem.Customers);
        }

这成功地创建了一个新行,我在对话框后面可以看到,但是,我有一个问题 - 显示的对话框是显示所有客户信息的另一个表单,这是通过文本框处理,其中数据绑定到数据库中的相应字段,因此它们自然会在数据库中显示所选记录的信息,但此时它显示的是先前所选记录的信息,而不是移动到使用上述代码创建的记录。

如何让系统创建新行,然后在打开新表格进行编辑之前关注新行?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

基本上,您可以将信息传递给对话框表单,该表单可用于导航到正确的记录。

最简单的方法是为接受参数的第二种形式创建构造函数。

public frmCustomer(int newID)
{
    InitializeComponent(); 
    // use newID (if present) to navigate
    // to the correct row
}

frmCustomer frmCust = new frmCustomer(theNewID);
frmCust.ShowDialog();

我不知道你的绑定细节,所以不能告诉你如何导航到记录。

还有许多其他方法,outlined here

一种简单的方法可能是在下一个新记录上打开对话框表单并导航回一行但是,我再也不知道你的设置足以为此提供代码。如果您有BindingSource,则可以使用其方法MoveLast()MovePrevious()

如果您使用第一种方法,并且第二种形式打开时使用传递的ID,那么您可以使用Find的{​​{1}}方法,然后使用BindingSource属性移至找到的记录。请参阅示例here at MSDN

相关问题