2013-03-28 6 views
2

У меня возникли проблемы с моим кодом относительно приложения Windows Form. У меня есть форма, которая требует от пользователя ввода некоторых данных для новой записи в базе данных. Я могу успешно создать новый «Заказ» в моей базе данных. После этого я хочу открыть форму, которая покажет пользователю все детали заказа. Поэтому я беру уже существующее окно и хочу, чтобы bindingSource переместился в определенную позицию. Мой код выглядит следующим образом:BindingSource.Position не влияет

«Форма NewOrder»

//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

Где вы инициализируете вас 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(); 
    } 
+0

Спасибо за вашу помощь, что помогает и сейчас отлично работает! – hoffmax91

Смежные вопросы