Access 2003、VS 2010 C#、Windows フォーム アプリケーション
テキストボックスなどは現在のデータにデータバインドされているため、この問題が発生していると思います。問題は、アプリケーションを閉じて再起動せずにバインディングソースに新しいデータを読み取らせる方法です。エラーはありません。いくつかの方法を試しましたが、アプリケーションが現在実行されている間はすべて新しいレコードが表示されないので、何が間違っていますか?
私のフォームロードは次のようになります..
this.fnDisplayPosition();
bdSource = new BindingSource();
string sql = "SELECT * FROM Table1";
OleDbDataAdapter da = new OleDbDataAdapter(sql, myCon);
DataSet ds = new DataSet();
da.Fill(ds, "Table1");
bdSource.DataSource = ds;
// this.table1BindingSource.AddNew();
txtID.DataBindings.Add("Text", bdSource, "ID");
cBAG.DataBindings.Add("Text", bdSource, "AgeGroup");
cBGender.DataBindings.Add("Text", bdSource, "Gender");
fnDisplayPosition メソッドは、データバインディングに関連付けられているレコードの数を読み取ります...
this.label7.Text = this.table1BindingSource.Position + 1 + " of " +
this.table1BindingSource.Count;
例として、これは私のナビゲーションボタンです..
this.table1BindingSource.MoveFirst();
this.fnDisplayPosition();
以下は私の挿入方法で、このウェブサイトから使用した新しいレコードを追加できます...
OleDbDataAdapter da = new OleDbDataAdapter(@"INSERT INTO Table1 (ID, AgeGroup, Gender)VALUES
(@txtID, @cBAG, @cBGender", myCon);
string qry = @"select * from Table1";
string upd = @"INSERT INTO Table1 (ID, AgeGroup, Gender)
VALUES(@txtID, @cBAG, @cBGender)";
myCon.Open();
try
{
da.SelectCommand = new OleDbCommand(qry, myCon);
DataSet ds = new DataSet();
da.Fill(ds, "Table1");
DataTable dt = ds.Tables["Table1"];
DataRow newRow = dt.NewRow();
newRow["ID"] = txtID;
newRow["AgeGroup"] = cBAG;
newRow["Gender"] = cBGender;
dt.Rows.Add(newRow);
OleDbCommand cmd = new OleDbCommand(upd, myCon);
cmd.Parameters.AddWithValue("@ID", txtID.Text);
cmd.Parameters.AddWithValue("@AgeGroup", cBAG.Text);
cmd.Parameters.AddWithValue("@Gender", cBGender.Text);
da.InsertCommand = cmd;
da.Update(ds, "Table1");
da.Fill(ds, upd);
} catch(Exception ex) {
Console.WriteLine("Error: " + ex);
} finally {
myCon.Close();
}