我在处理有关 Windows 窗体应用程序的代码时遇到了一些问题。 我有一个表单,要求用户在数据库中为新记录输入一些数据。我可以在我的数据库中成功创建一个新的“订单”。这样做之后,我想打开一个向用户显示订单所有详细信息的表单。因此,我使用一个已经存在的窗口并希望 bindingSource 跳转到某个位置。 我的代码如下:
“新订单”
//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();
我正在调用的“订单”构造函数:
public Order(int newIndex)
{
//initialize
InitializeComponent();
//set index and repaint
this.ordersBindingSource.Position = newIndex;
this.ordersBindingSource.ResetCurrentItem();
}
这根本不起作用,我得到了数据集的第一个条目。 我做错了什么?
最佳答案
你在哪里从“订单”中初始化你的 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();
}
关于c# - BindingSource.Position 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15693948/