hoffmax91
hoffmax91

Reputation: 131

BindingSource.Position has no effect

I'm having some trouble with my code regarding a Windows Form Application. I have a form, that requires the user to enter some data for a new record in the database. I can successfully create a new "Order" in my Database. After I do that, I want to open a form that shows the user all details of the order. Therefore I take an already existing window and want the bindingSource to jump to a certain position. My Code is as follows:

The "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();

The "Order form" constructor i'm calling:

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

This is simply not working and I get the first entry of the dataset. What am I doing wrong?

Upvotes: 2

Views: 2472

Answers (1)

user2122730
user2122730

Reputation: 21

Where do you initialize you BindingSource from "Order form"? Make sure your newIndex <= ordersBindingSource.Count().

Try this:

    //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();
    }

Upvotes: 1

Related Questions