c# - BindingSource.Position 无效

标签 c# winforms bindingsource

我在处理有关 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/

相关文章:

C# linq 列表查找最接近的数字

c# - 为什么显示下拉列表需要在 DataGridView 中单击两次?

c# - 找不到引用契约(Contract)的默认端点元素 - 托管 wcf

c# - 绑定(bind)源刷新

c# - 在 WPF 中的 UserControl 中设置窗口所有者

使用另一种形式更改datagridview字体大小时C#异常错误

c# - Datagridview 错误 System.IndexOutOfRangeException : Index 0 does not have a value

winforms - 如何将 BindingSource 当前记录设置为 null?

c# - 使用javascript选中和取消选中转发器控件中的复选框?

c# - 如何检查 DataGridView 中的空单元格值?