C#应用中DataGridView无法刷新Access数据库新增数据的问题求助
解决DataGridView无法显示Access新增记录的问题
你的核心问题是:triplePlayBindingSource绑定的是程序启动时加载的旧数据集,插入新记录后这个数据源不会自动同步数据库的最新内容。调用Update()和Refresh()只是刷新控件的绘制逻辑,不会重新从数据库获取数据。
下面提供两种可行的解决方案:
方案1:重新加载数据源(简单直接)
把加载数据的逻辑抽成可复用的方法,插入成功后重新查询数据库并绑定数据源。
步骤1:编写数据加载方法
private void LoadTriplePlayData() { try { // 确保连接已关闭 if (conn.State == ConnectionState.Open) conn.Close(); conn.Open(); // 查询全表数据 OleDbCommand cmd = new OleDbCommand("SELECT * FROM TriplePlay", conn); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmd); DataTable dataTable = new DataTable(); dataAdapter.Fill(dataTable); // 重新绑定数据源 triplePlayBindingSource.DataSource = dataTable; dgvJackpotTriplePlay.DataSource = triplePlayBindingSource; } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // 确保连接最终关闭 if (conn.State == ConnectionState.Open) conn.Close(); } }
步骤2:修改按钮点击事件
插入成功后调用加载方法,并滚动到最新记录:
private void btnAddRecord_Click (object sender, EventArgs e) { DateTime drawDate; int ball1, ball2, ball3, ball4, ball5, ball6; drawDate = dtpDrawingData.Value; ball1 = int.Parse(txtBall1.Text); ball2 = int.Parse(txtBall2.Text); ball3 = int.Parse(txtBall3.Text); ball4 = int.Parse(txtBall4.Text); ball5 = int.Parse(txtBall5.Text); ball6 = int.Parse(txtBall6.Text); try { conn.Open (); OleDbCommand cmd = conn.CreateCommand (); cmd.CommandType = CommandType.Text; // 改用参数化查询,避免SQL注入和日期格式问题 cmd.CommandText = "INSERT INTO TriplePlay (DrawDate, Ball1, Ball2, Ball3, Ball4, Ball5, Ball6) VALUES (?, ?, ?, ?, ?, ?, ?)"; cmd.Parameters.AddWithValue("@DrawDate", drawDate); cmd.Parameters.AddWithValue("@Ball1", ball1); cmd.Parameters.AddWithValue("@Ball2", ball2); cmd.Parameters.AddWithValue("@Ball3", ball3); cmd.Parameters.AddWithValue("@Ball4", ball4); cmd.Parameters.AddWithValue("@Ball5", ball5); cmd.Parameters.AddWithValue("@Ball6", ball6); cmd.ExecuteNonQuery (); MessageBox.Show("Record Added", "Update Made", MessageBoxButtons.OK, MessageBoxIcon.Information); // 重新加载数据 LoadTriplePlayData(); // 滚动到最后一条记录 triplePlayBindingSource.MoveLast(); } catch (Exception ex) { MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); } finally { if (conn.State == ConnectionState.Open) conn.Close(); } }
方案2:手动添加记录到BindingSource(高效)
如果数据库数据量大,重新查询会消耗资源,可以直接把新增记录加到已绑定的DataTable中:
private void btnAddRecord_Click (object sender, EventArgs e) { DateTime drawDate; int ball1, ball2, ball3, ball4, ball5, ball6; drawDate = dtpDrawingData.Value; ball1 = int.Parse(txtBall1.Text); ball2 = int.Parse(txtBall2.Text); ball3 = int.Parse(txtBall3.Text); ball4 = int.Parse(txtBall4.Text); ball5 = int.Parse(txtBall5.Text); ball6 = int.Parse(txtBall6.Text); try { conn.Open (); OleDbCommand cmd = conn.CreateCommand (); cmd.CommandType = CommandType.Text; // 参数化查询 cmd.CommandText = "INSERT INTO TriplePlay (DrawDate, Ball1, Ball2, Ball3, Ball4, Ball5, Ball6) VALUES (?, ?, ?, ?, ?, ?, ?)"; cmd.Parameters.AddWithValue("@DrawDate", drawDate); cmd.Parameters.AddWithValue("@Ball1", ball1); cmd.Parameters.AddWithValue("@Ball2", ball2); cmd.Parameters.AddWithValue("@Ball3", ball3); cmd.Parameters.AddWithValue("@Ball4", ball4); cmd.Parameters.AddWithValue("@Ball5", ball5); cmd.Parameters.AddWithValue("@Ball6", ball6); cmd.ExecuteNonQuery (); MessageBox.Show("Record Added", "Update Made", MessageBoxButtons.OK, MessageBoxIcon.Information); // 获取绑定的DataTable DataTable dataTable = (DataTable)triplePlayBindingSource.DataSource; // 创建新行并赋值 DataRow newRow = dataTable.NewRow(); newRow["DrawDate"] = drawDate; newRow["Ball1"] = ball1; newRow["Ball2"] = ball2; newRow["Ball3"] = ball3; newRow["Ball4"] = ball4; newRow["Ball5"] = ball5; newRow["Ball6"] = ball6; // 添加到DataTable dataTable.Rows.Add(newRow); // 通知数据源更新 triplePlayBindingSource.ResetBindings(false); // 滚动到最新记录 triplePlayBindingSource.MoveLast(); } catch (Exception ex) { MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); } finally { if (conn.State == ConnectionState.Open) conn.Close(); } }
关键注意事项
- 必须使用参数化查询:避免SQL注入风险,同时解决日期、特殊字符导致的SQL语法错误。
- 用
finally块管理数据库连接:确保无论是否抛出异常,连接都能正确关闭。
内容的提问来源于stack exchange,提问作者David F
相关产品推荐
相关产品推荐

