插入数据库后如何刷新Datagridview [英] How I Can To Refresh Datagridview After Insert To The Database
问题描述
你好,
如何在插入数据库后刷新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);
}
// 插入代码
< span class =code-keyword> 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 + < span class =code-string>',' + 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);
}
// 插入代码
< span class =code-keyword> 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 + < span class =code-string>',' + 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)
{
// < span class =code-comment>刷新
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 work
but 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 detials
the 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 thedatagridview
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!