代码不遵循源代码中的顺序

Code not following sequential order in source

我有一个设置表单,上面有一个组合框,我想在加载表单时使用从访问数据库返回的数据填充该组合框。

我遇到困难的地方实际上是设置组合框的数据源 - 当程序执行时,它从填充 OleDbDataAdapter 跳转到加载表单;跳过代码。

这是我的最新代码:

表单加载

        private void frm_settings_Load(object sender, EventArgs e)
    {
        Divisions divs = new Divisions();
        DataSet ds = new DataSet();


        ds = divs.GetActiveDivisions(); //jumps from here to loading the form
        this.cmbo_divisions.DataSource = ds; //this never gets invoked
    }

和师部 class

    class Divisions
{
    private string error;

    public string Error //read only
    {
        get { return this.error; }
    }

    public DataSet GetActiveDivisions()
    {
        OleDbConnection conn = new OleDbConnection();

        PinnacleConnection Pconn = new PinnacleConnection();
        string sql = "SELECT ID, title FROM Divisions WHERE active = true;";
        DataSet ds = new DataSet();
        //connect to db
        conn = Pconn.createConnection();

        try
        {
            conn.Open();
        }
        catch (Exception ex)
        {
            //error handling here
            this.error = ex.Message;
            return ds;
        }

        OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
        adapter.Fill(ds); //!!jumps from here to loading the form!!
        conn.Close(); //never gets invoked?
        return ds; //never gets invoked?
    }
}

正如您在我的评论中看到的那样,执行跳过了 cmbo_divisions 对象上的数据源设置...导致一个空的组合框。

我很茫然,任何帮助将不胜感激。

我最好的猜测是 adapter.Fill 抛出异常或包含消息泵。所以先测试这些场景。

您不需要使用数据集,数据表更适合:

DataTable dt= new DataTable();
...
adapter.Fill(dt); 

并且绑定将直接绑定到数据表,因此您可以尽可能避免使用数据集,因为它们是更重的对象:

this.cmbo_divisions.DataSource = dt;