本文介绍了插入数据库后如何刷新Datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好, 如何在插入数据库后刷新datagridview,(每次点击按钮时,只显示在网格视图不是当前的) private void button2_Click( object sender,EventArgs e) { // 插入数据 // 。 ..最后一个条形码 string JSS_connetionString001 = null ; OleDbConnection JSS_cnn001; OleDbCommand JSS_cmd001; string JSS_sql001 = null ; OleDbDataReader JSS_reader001; JSS_connetionString001 = Provider = Microsoft.Jet.OLEDB.4.0; Data Source = | DataDirectory | / je_salestoredb。 mdb; Persist Security Info = True; Jet OLEDB:数据库密码= JE2014; JSS_sql001 = 从员工中选择*,其中employee_ID =(从员工中选择max(employee_ID)); JSS_cnn001 = new OleDbConnection(JSS_connetionString001); 尝试 { JSS_cnn001.Open(); JSS_cmd001 = new OleDbCommand(JSS_sql001,JSS_cnn001); JSS_reader001 = JSS_cmd001.ExecuteReader(); while (JSS_reader001.Read()) { label01.Text =(JSS_reader001.GetValue( 0 )+ ); } JSS_reader001.Close(); JSS_cmd001.Dispose(); JSS_cnn001.Close(); } catch (例外情况) { 返回; } // 检查是否有ID是空的 如果(label01.Text == ) { label01.Text = 0000001; } else { double JSS_prductG_ID = System.Convert.ToDouble(label01.Text)+ 0000001 ; label01.Text = JSS_prductG_ID.ToString( 0000000); } // 插入代码 string JSS_connetionString002 = null ; OleDbConnection JSS_connection002; OleDbDataAdapter JSS_oledbAdapter002 = new OleDbDataAdapter(); string JSS_sql002 = null ; JSS_connetionString002 = Provider = Microsoft.Jet.OLEDB.4.0; Data Source = | DataDirectory | / je_salestoredb。 mdb; Persist Security Info = True; Jet OLEDB:数据库密码= JE2014; JSS_connection002 = new OleDbConnection(JSS_connetionString002); JSS_sql002 = 插入员工(employee_ID,employee_CODE,employee_NAME,employee_NATIONAL_CODE,employee_CITY,employee_STATE,employee_VILLAGE,employee_TEL1 ,employee_TEL2,employee_EMAIL,employee_SALARY,employee_WORK,employee_SYSTEM_Authority,employee_PASS,employee_JOIN_DATE,employee_EDIT_DATE,employee_USER)values(' + label01.Text + ',' + textbox02.Text + ',' + textbox03.Text + ',' + textbox04.Text + ',' + textbox05.Text + ',' + textbox06.Text + ',' + textbox07.Text + ',' + textbox08.Text + ' ,' + textbox09.Text + ',' + textbox10.Text + ',' + textbox11.Text + ',' + textbox12.Text + ',' + label13.Text + ',' + textbox14。文字+ ',' + textbox15.Text + ',' + textbox16.Text + ',' + label17.Text + '); 尝试 { JSS_connection002.Open(); JSS_oledbAdapter002.InsertCommand = new OleDbCommand(JSS_sql002,JSS_connection002); JSS_oledbAdapter002.InsertCommand.ExecuteNonQuery(); } catch (例外情况) { 返回; } // 刷新 employeesDataGridView.Refresh(); string JSS_connetionString011 = Provider = Microsoft.Jet.OLEDB.4.0; Data Source = | DataDirectory | /je_salestoredb.mdb; Persist Security Info = True; Jet OLEDB:Database Password = JE2014; string JSS_sql011 = select * from employees ; OleDbConnection JSS_connection011 = new OleDbConnection(JSS_connetionString011); OleDbDataAdapter JSS_dataadapter011 = new OleDbDataAdapter(JSS_sql011,JSS_connection011); DataSet JSS_ds011 = new DataSet(); JSS_ds011.Clear(); 尝试 { JSS_connection011.Open(); JSS_dataadapter011.Fill(JSS_ds011, employees); JSS_connection011.Close(); employeesDataGridView.DataSource = JSS_ds011; employeesDataGridView.DataMember = employees; } catch (例外情况) { 返回; } employeesDataGridView.Refresh(); } 此代码不起作用 但是用了两个按钮才能工作 private void button2_Click( object sender ,EventArgs e) { // 插入数据 // ...最后一个条形码 string JSS_connetionString001 = null ; OleDbConnection JSS_cnn001; OleDbCommand JSS_cmd001; string JSS_sql001 = null ; OleDbDataReader JSS_reader001; JSS_connetionString001 = Provider = Microsoft.Jet.OLEDB.4.0; Data Source = | DataDirectory | / je_salestoredb。 mdb; Persist Security Info = True; Jet OLEDB:数据库密码= JE2014; JSS_sql001 = 从员工中选择*,其中employee_ID =(从员工中选择max(employee_ID)); JSS_cnn001 = new OleDbConnection(JSS_connetionString001); 尝试 { JSS_cnn001.Open(); JSS_cmd001 = new OleDbCommand(JSS_sql001,JSS_cnn001); JSS_reader001 = JSS_cmd001.ExecuteReader(); while (JSS_reader001.Read()) { label01.Text =(JSS_reader001.GetValue( 0 )+ ); } JSS_reader001.Close(); JSS_cmd001.Dispose(); JSS_cnn001.Close(); } catch (例外情况) { 返回; } // 检查是否有ID是空的 如果(label01.Text == ) { label01.Text = 0000001; } else { double JSS_prductG_ID = System.Convert.ToDouble(label01.Text)+ 0000001 ; label01.Text = JSS_prductG_ID.ToString( 0000000); } // 插入代码 string JSS_connetionString002 = null ; OleDbConnection JSS_connection002; OleDbDataAdapter JSS_oledbAdapter002 = new OleDbDataAdapter(); string JSS_sql002 = null ; JSS_connetionString002 = Provider = Microsoft.Jet.OLEDB.4.0; Data Source = | DataDirectory | / je_salestoredb。 mdb; Persist Security Info = True; Jet OLEDB:数据库密码= JE2014; JSS_connection002 = new OleDbConnection(JSS_connetionString002); JSS_sql002 = 插入员工(employee_ID,employee_CODE,employee_NAME,employee_NATIONAL_CODE,employee_CITY,employee_STATE,employee_VILLAGE,employee_TEL1 ,employee_TEL2,employee_EMAIL,employee_SALARY,employee_WORK,employee_SYSTEM_Authority,employee_PASS,employee_JOIN_DATE,employee_EDIT_DATE,employee_USER)values(' + label01.Text + ',' + textbox02.Text + ',' + textbox03.Text + ',' + textbox04.Text + ',' + textbox05.Text + ',' + textbox06.Text + ',' + textbox07.Text + ',' + textbox08.Text + ' ,' + textbox09.Text + ',' + textbox10.Text + ',' + textbox11.Text + ',' + textbox12.Text + ',' + label13.Text + ',' + textbox14。文字+ ',' + textbox15.Text + ',' + textbox16.Text + ',' + label17.Text + '); 尝试 { JSS_connection002.Open(); JSS_oledbAdapter002.InsertCommand = new OleDbCommand(JSS_sql002,JSS_connection002); JSS_oledbAdapter002.InsertCommand.ExecuteNonQuery(); } catch (例外情况) { 返回; } } 私人 void button3_Click( object sender,EventArgs e) { // 刷新 employeesDataGridView.Refresh(); string JSS_connetionString011 = Provider = Microsoft.Jet.OLEDB.4.0; Data Source = | DataDirectory | /je_salestoredb.mdb; Persist Security Info = True; Jet OLEDB:Database Password = JE2014; string JSS_sql011 = select * from employees ; OleDbConnection JSS_connection011 = new OleDbConnection(JSS_connetionString011); OleDbDataAdapter JSS_dataadapter011 = new OleDbDataAdapter(JSS_sql011,JSS_connection011); DataSet JSS_ds011 = new DataSet(); JSS_ds011.Clear(); 尝试 { JSS_connection011.Open(); JSS_dataadapter011.Fill(JSS_ds011, employees); JSS_connection011.Close(); employeesDataGridView.DataSource = JSS_ds011; employeesDataGridView.DataMember = employees; } catch (例外情况) { 返回; } employeesDataGridView.Refresh(); } 其他detials 第一个代码刷新数据网格视图,但按下按钮两次并且不显示当前输入但是如果放入单独按钮事件,表格闪烁(显示新值并快速消失,除下次时不显示)解决方案 保存绑定数据到gridView后。 ..employeesDataGridview.DataBind(); 您需要做的就是在执行任何操作后重新绑定 datagridview 。检查这个答案(由询问者提供) 如何在vb.net中刷新datagridview [ ^ ] 数据不会以这种方式刷新。 添加新记录后,更新现有记录或说删除记录,您必须再次将数据绑定到gridview 以反映更改。 -KR hello,how i can to refresh datagridview after insert to the database , ( in every time click on the button , the Previous record only that appear in the grid view not the Current) private void button2_Click(object sender, EventArgs e) {//insert data // ... last barcode string JSS_connetionString001 = null; OleDbConnection JSS_cnn001; OleDbCommand JSS_cmd001; string JSS_sql001 = null; OleDbDataReader JSS_reader001; JSS_connetionString001 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014"; JSS_sql001 = "select * from employees where employee_ID=(select max(employee_ID) from employees)"; JSS_cnn001 = new OleDbConnection(JSS_connetionString001); try { JSS_cnn001.Open(); JSS_cmd001 = new OleDbCommand(JSS_sql001, JSS_cnn001); JSS_reader001 = JSS_cmd001.ExecuteReader(); while (JSS_reader001.Read()) { label01.Text = (JSS_reader001.GetValue(0) + ""); } JSS_reader001.Close(); JSS_cmd001.Dispose(); JSS_cnn001.Close(); } catch (Exception ex) { return; } // check if ID is Null if (label01.Text == "") { label01.Text = "0000001"; } else { double JSS_prductG_ID = System.Convert.ToDouble(label01.Text) + 0000001; label01.Text = JSS_prductG_ID.ToString("0000000"); } //insert code string JSS_connetionString002 = null; OleDbConnection JSS_connection002; OleDbDataAdapter JSS_oledbAdapter002 = new OleDbDataAdapter(); string JSS_sql002 = null; JSS_connetionString002 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014"; JSS_connection002 = new OleDbConnection(JSS_connetionString002); JSS_sql002 = "insert into employees(employee_ID,employee_CODE,employee_NAME,employee_NATIONAL_CODE,employee_CITY,employee_STATE,employee_VILLAGE,employee_TEL1,employee_TEL2,employee_EMAIL,employee_SALARY,employee_WORK,employee_SYSTEM_Authority,employee_PASS,employee_JOIN_DATE,employee_EDIT_DATE,employee_USER) values('" + label01.Text + "','" + textbox02.Text + "','" + textbox03.Text + "','" + textbox04.Text + "','" + textbox05.Text + "','" + textbox06.Text + "','" + textbox07.Text + "','" + textbox08.Text + "','" + textbox09.Text + "','" + textbox10.Text + "','" + textbox11.Text + "','" + textbox12.Text + "','" + label13.Text + "','" + textbox14.Text + "','" + textbox15.Text + "','" + textbox16.Text + "','" + label17.Text + "')"; try { JSS_connection002.Open(); JSS_oledbAdapter002.InsertCommand = new OleDbCommand(JSS_sql002, JSS_connection002); JSS_oledbAdapter002.InsertCommand.ExecuteNonQuery(); } catch (Exception ex) { return; } //Refresh employeesDataGridView.Refresh(); string JSS_connetionString011 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014"; string JSS_sql011 = "select * from employees"; OleDbConnection JSS_connection011 = new OleDbConnection(JSS_connetionString011); OleDbDataAdapter JSS_dataadapter011 = new OleDbDataAdapter(JSS_sql011, JSS_connection011); DataSet JSS_ds011 = new DataSet(); JSS_ds011.Clear(); try { JSS_connection011.Open(); JSS_dataadapter011.Fill(JSS_ds011, "employees"); JSS_connection011.Close(); employeesDataGridView.DataSource = JSS_ds011; employeesDataGridView.DataMember = "employees"; } catch (Exception ex) { return; } employeesDataGridView.Refresh();}this code don't workbut by used two button is work will private void button2_Click(object sender, EventArgs e) { //insert data // ... last barcode string JSS_connetionString001 = null; OleDbConnection JSS_cnn001; OleDbCommand JSS_cmd001; string JSS_sql001 = null; OleDbDataReader JSS_reader001; JSS_connetionString001 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014"; JSS_sql001 = "select * from employees where employee_ID=(select max(employee_ID) from employees)"; JSS_cnn001 = new OleDbConnection(JSS_connetionString001); try { JSS_cnn001.Open(); JSS_cmd001 = new OleDbCommand(JSS_sql001, JSS_cnn001); JSS_reader001 = JSS_cmd001.ExecuteReader(); while (JSS_reader001.Read()) { label01.Text = (JSS_reader001.GetValue(0) + ""); } JSS_reader001.Close(); JSS_cmd001.Dispose(); JSS_cnn001.Close(); } catch (Exception ex) { return; } // check if ID is Null if (label01.Text == "") { label01.Text = "0000001"; } else { double JSS_prductG_ID = System.Convert.ToDouble(label01.Text) + 0000001; label01.Text = JSS_prductG_ID.ToString("0000000"); } //insert code string JSS_connetionString002 = null; OleDbConnection JSS_connection002; OleDbDataAdapter JSS_oledbAdapter002 = new OleDbDataAdapter(); string JSS_sql002 = null; JSS_connetionString002 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014"; JSS_connection002 = new OleDbConnection(JSS_connetionString002); JSS_sql002 = "insert into employees(employee_ID,employee_CODE,employee_NAME,employee_NATIONAL_CODE,employee_CITY,employee_STATE,employee_VILLAGE,employee_TEL1,employee_TEL2,employee_EMAIL,employee_SALARY,employee_WORK,employee_SYSTEM_Authority,employee_PASS,employee_JOIN_DATE,employee_EDIT_DATE,employee_USER) values('" + label01.Text + "','" + textbox02.Text + "','" + textbox03.Text + "','" + textbox04.Text + "','" + textbox05.Text + "','" + textbox06.Text + "','" + textbox07.Text + "','" + textbox08.Text + "','" + textbox09.Text + "','" + textbox10.Text + "','" + textbox11.Text + "','" + textbox12.Text + "','" + label13.Text + "','" + textbox14.Text + "','" + textbox15.Text + "','" + textbox16.Text + "','" + label17.Text + "')"; try { JSS_connection002.Open(); JSS_oledbAdapter002.InsertCommand = new OleDbCommand(JSS_sql002, JSS_connection002); JSS_oledbAdapter002.InsertCommand.ExecuteNonQuery(); } catch (Exception ex) { return; }} private void button3_Click(object sender, EventArgs e) { //Refresh employeesDataGridView.Refresh(); string JSS_connetionString011 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014"; string JSS_sql011 = "select * from employees"; OleDbConnection JSS_connection011 = new OleDbConnection(JSS_connetionString011); OleDbDataAdapter JSS_dataadapter011 = new OleDbDataAdapter(JSS_sql011, JSS_connection011); DataSet JSS_ds011 = new DataSet(); JSS_ds011.Clear(); try { JSS_connection011.Open(); JSS_dataadapter011.Fill(JSS_ds011, "employees"); JSS_connection011.Close(); employeesDataGridView.DataSource = JSS_ds011; employeesDataGridView.DataMember = "employees"; } catch (Exception ex) { return; } employeesDataGridView.Refresh(); }other detialsthe first code make refresh to data grid veiw, but after pressing the button twice and don't show the current input but it works well if it is put in a separate button Event,and the table flashed (show the new value and disappear quickly and does not show except in the next time) 解决方案 After Saving Bind data to gridView...employeesDataGridview.DataBind();All you have to do is rebind the datagridview after any action. Check this answer(which is by enquirer)how to refresh datagridview in vb.net[^]Data won't be refreshed this way.After adding new record, updating an existing record or say deleting a record, you must bind the data again to the gridview in order to reflect the changes.-KR 这篇关于插入数据库后如何刷新Datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 19:06