BindingSource.Position无效

时间:2013-03-28 23:29:38

标签: c# winforms bindingsource

我在使用Windows窗体应用程序的代码时遇到了一些问题。 我有一个表单,要求用户为数据库中的新记录输入一些数据。我可以在我的数据库中成功创建一个新的“订单”。在我这样做之后,我想打开一个表单,向用户显示订单的所有详细信息。因此,我采用一个已经存在的窗口,并希望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();

我正在调用的“订单表单”构造函数:

public Order(int newIndex)
{
    //initialize
    InitializeComponent();
    //set index and repaint
    this.ordersBindingSource.Position = newIndex;
    this.ordersBindingSource.ResetCurrentItem();
}

这根本不起作用,我得到了数据集的第一个条目。 我做错了什么?

1 个答案:

答案 0 :(得分:1)

在哪里从“订购单”中初始化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();
    }