从ChildForm添加数据后,ComboBox刷新

时间:2009-02-04 17:21:00

标签: c# combobox refresh

在向Access数据库添加新数据后,我似乎无法刷新我的ComboBox。

以下是我用来添加新数据的代码:

private void btnAddUser_Click(object sender, EventArgs e)
{
        AccountForm actFrm = new AccountForm();

        if (actFrm.ShowDialog() == DialogResult.OK)
        {
            try
            {
                this.oleDbDataAdapter1.InsertCommand.CommandText =
                    "INSERT INTO userTable (AccountName, Username, PopServer, PopPort, Psswrd, SmtpServer, SmtpPort, Email)" +
                    "VALUES     ('" + actFrm.txtAccName.Text + "','" + actFrm.txtUsername.Text + "','" + actFrm.txtPop3.Text + "','" + actFrm.txtPop3Port.Text + "','" + actFrm.txtPassword.Text + "','" + actFrm.txtSmtp.Text + "','" + actFrm.txtSmtpPort.Text + "','" + actFrm.txtEmail.Text + "')";

                //open the bridge between the application and the datasource
                this.oleDbConnection1.Open();

                this.oleDbDataAdapter1.InsertCommand.Connection = oleDbConnection1;

                //execute the query 
                this.oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();

                //close the connection
                this.oleDbConnection1.Close();

                MessageBox.Show("User Added Successfully");  //inform the user
                //tried here to refresh and even open close the myConn connection. to no avail
            }
            catch (System.Data.OleDb.OleDbException exp)
            {
                //close the connection
                this.oleDbConnection1.Close();

                MessageBox.Show(exp.ToString());
            }
      }
}

2 个答案:

答案 0 :(得分:2)

您的BindingSource是否使用DataSet?如果是这样,您需要通过DataSet进行插入,并且可能刷新绑定源。如果你这样做,你也可以避免重复的插入逻辑。

或者,你可以刷新DataSet,但是这种方法没有利用DataSet的功能,并且会导致很多重复的代码。

答案 1 :(得分:0)

我最近在VB.NET中遇到了同样的问题。我发现将数据设置为Nothing(我相信C#中为null),然后将其分配给我的对象列表解决了问题。

  attendanceDataFiles.DataSource = Nothing
  attendanceDataFiles.DataSource = myFileList.files