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# - 在将 OnModelCreating 与 ApplicationDbContext 一起使用时解决 'No key Defined' 错误?

c# - 打印质量 winform

C# 相当于 VB.NET 'Handles button1.Click, button2.Click'

c# - 有没有什么办法可以聚焦(选项卡控件)图片框?

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

c# - 在 ASP.NET 中 ConfigureAwait(false) 的行为是什么?

c# - 如何使用匿名对象创建动态 Linq Select 表达式

c# - DataTable.Select 与 List.FindAll

c# - datagridview 中的每一行(对象)都有自己的绑定(bind)源?

c# - BindingSource CurrentChanged/PositionChanged 旧项