BindingSource.位置没有影响
本文关键字:有影响 位置 BindingSource | 更新日期: 2023-09-27 18:06:19
我有一些麻烦与我的代码关于Windows窗体应用程序。我有一个表单,它要求用户在数据库中输入一些新记录的数据。我可以成功地在我的数据库中创建一个新的"Order"。这样做之后,我想打开一个表单,向用户显示订单的所有细节。因此,我采取一个已经存在的窗口,并希望bindingSource跳转到某个位置。我的代码如下:
"newOrder form"
//do stuff for the creation
//open a customerDetails window with the new entry
//resolve index of the new item
int newIndex = ordersBindingSource.Find("OrderID", newOrderID);
//create a new window and pass the index in the constructor
Order orderDetailsView = new Order(newIndex);
//show the new window and dispose the old one
orderDetailsView.Show();
this.Close();
this.Dispose();
我调用的"Order form"构造函数:
public Order(int newIndex)
{
//initialize
InitializeComponent();
//set index and repaint
this.ordersBindingSource.Position = newIndex;
this.ordersBindingSource.ResetCurrentItem();
}
这根本不起作用,我得到了数据集的第一个条目。我做错了什么?
从"Order form"初始化你的BindingSource在哪里?确保你的newIndex <= ordersBindingSource.Count()。
试试这个:
//Form Order
int currentOrder = 0;
//constructor
public Order(int newIndex)
{
//initialize your variable here
currentOrder = newIndex;
//initialize
InitializeComponent();
}
//Create a void method that sets the BindingSource position
void SetupForm()
{
ordersBindingSource.Position = currentOrder;
}
// Override the OnLoad event and call the SetupForm() method here
protected override OnLoad(EventArgs e)
{
SetupForm();
}