Windows 窗体应用程序,VS 2010 C#,Access 2003
我从这个网站引用,here。我的目标是显示更新的记录(插入/删除更新),而无需关闭并重新启动应用程序以显示新的反射。我有很多文本框、组合框等,所以我没有使用 datagridview。
从那个网站有两段代码......
bindingNavigator1.BindingSource = bdSource;
this.dataGridView1.DataSource = bdSource;
所以我做了这个...
this.table1BindingSource = dbSource;
并给出“对象引用未设置为对象实例”的错误。警告说dbSource 永远不会被分配,并且会有一个默认值 null。
我的插入命令参数结构..
OleDbCommand cmd = new OleDbCommand(@"INSERT INTO Table1 (ID, Name) VALUES(@ID, @Name)", myCon);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", txtID.Text);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
myCon.Open();
cmd.ExecuteNonQuery();
myCon.Close();
以下方法我希望数据会被刷新......
private void btnReload_Click(object sender, EventArgs e)
{
OleDbDataAdapter.Update(dtSource);
}
和...
public partial class Form1 : Form
{
private OleDbConnection myCon;
private string connectionString;
private string commandText;
private OleDbCommandBuilder OleDbCommandBuilder;
private OleDbDataAdapter OleDbDataAdapter;
private DataTable dtSource;
private BindingSource dbSource;
public Form1()
{
connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data.. ";
commandText = "SELECT * FROM TABLE1";
myCon = new OleDbConnection(connectionString);
OleDbDataAdapter = new OleDbDataAdapter(commandText, myCon);
OleDbCommandBuilder = new OleDbCommandBuilder(OleDbDataAdapter);
dtSource = new DataTable();
OleDbDataAdapter.Fill(dtSource);
dbSource = new BindingSource();
dbSource.DataSource = dtSource;
提前感谢任何可以帮助我的人
编辑 我也有导航按钮..
private void fnDisplayPosition()
{
this.label2.Text = this.table1BindingSource.Position + 1 + " of " +
this.table1BindingSource.Count;
table1BindingSource.ResetBindings(false);
}
然后在表单加载...
private void Form1_Load(object sender, EventArgs e)
{
this.fnDisplayPosition();
}
导航按钮示例...
private void btnFirst_Click(object sender, EventArgs e)
{
this.table1BindingSource.MoveFirst();
this.fnDisplayPosition();
}
更新 2 我在编码中犯了一些错误,所以警告标志和错误都消失了