本文介绍了如何更新组合框,以便在将其添加到数据库后立即显示该记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨! 这是我的代码,用于汇总数据库中的数据库。 Main_Load( object sender,EventArgs e) { bindComboBox(); } public void bindComboBox() { DataTable table = new DataTable(); conn.ConnectionString = connString; conn.Open(); SqlDataAdapter da = new SqlDataAdapter( SELECT * FROM KombiStateTbl,conn); da.Fill(table); kombiComboBox.DataSource = new BindingSource(table, null ); kombiComboBox.DisplayMember = NumberPlate; kombiComboBox.ValueMember = NumberPlate; conn.Close(); } 来自另一种表格的我可以将另一条记录添加到数据库中。我的问题是:如何更新组合框,以便在将其添加到数据库后立即显示该记录? 这是添加记录的代码: private void okButton_Click( object sender,EventArgs e) { int intInsert = 0 ; 字符串 connString =( @ 数据源= .\SQLEXPRESS; AttachDbFilename = D:\Projects\RentalPlus\RentalPlus\KombiDB.mdf; Integrated Security = True; User Instance = True); SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = INSERT INTO KombiStateTbl(NumberPlate,LicenseExpiryDate,ControlDate, + Documentos,FaróisLanternas,EstepeMacacoChaveDeRoda,Triângulo,Extintor,Acendedor, + RodasCalota,Retrovisores,Párabrisa,BancosTapetes,Pneus,ParachoqueDianteiro, + ParachoqueTraseiro,PortaDianteiraDireitaM,PortaDianteiraEsquerdaP,PortaLateral, + LateralEsquerda,LateralEsquerdaTraseira,LateralDireitaTraseira,ParalamaTraseiroDireito, + ParalamaTraseiroEsquerdo,Teto,PortaDoPortaMalas ,PortaTraseiraDoMotor,Traseira, + Dianteira,FaróisDianteiros,FaroisTraseiros,CurrentPic,CurrentPicPath) VALUES + (@ Param1,@ Param2,@ Param3,'OK', 'OK','OK','OK','OK','OK','OK','OK','OK','OK', + 确定,确定,确定,确定,确定,确定,确定, OK','OK','OK','OK','OK','OK','OK', + 'OK','OK','OK','OK',NULL,NULL); cmd.Parameters.Add( @ Param1,SqlDbType.Char ).Value = noPlateTextBox.Text; cmd.Parameters.Add( @ Param2,SqlDbType.Char).Value = licenseDateTimePicker.Text; cmd.Parameters.Add( @ Param3,SqlDbType.Char).Value = controlDateTimePicker.Text; cmd.Connection = conn; conn.Open(); intInsert = cmd.ExecuteNonQuery(); conn.Close(); if (intInsert!= 0 ) { MessageBox.Show( 数据插入成功, 成功,MessageBoxButtons.OK, MessageBoxIcon.Information); noPlateTextBox.Text = ; okButton.Enabled = false ; } else { MessageBox.Show( 数据插入失败, 错误,MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); } } 请帮忙。解决方案 你需要做一个重新加载以重新收集更新的内容,然后在添加该内容后更新组合框的数据源。 这在if语句中应修复: if (intInsert!= 0 ) { MessageBox.Show( 数据插入成功, 成功,MessageBoxButtons.OK, MessageBoxIcon.Information); noPlateTextBox.Text = ; okButton.Enabled = false ; bindComboBox(); // << - 更新后重新收集组合框的内容。 } 对不起,我唯一一个调用上面绑定行的错误。 只需添加在ok_Button.Enabled = false之后,在ok按钮内调用if条件; 所以你的okButton_Click看起来像这样: private void okButton_Click(object sender,EventArgs e) { int intInsert = 0; String connString =(@Data Source = .\SQLEXPRESS; AttachDbFilename = D:\Projects\RentalPlus\RentalPlus\KombiDB.mdf; Integrated Security = True; User Instance = True); SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText =INSERT INTO KombiStateTbl(NumberPlate,LicenseExpiryDate,ControlDate, +Documentos,FaróisLanternas,EstepeMacacoChaveDeRoda,Triângulo,Extintor,Acendedor, +RodasCalota,Retrovisores,Párabrisa ,BancosTapetes,Pneus,ParachoqueDianteiro, +ParachoqueTraseiro,PortaDianteiraDireitaM,PortaDianteiraEsquerdaP,PortaLateral, +LateralEsquerda,LateralEsquerdaTraseira,LateralDireitaTraseira,ParalamaTraseiroDireito, +ParalamaTraseiroEsquerdo,Teto,PortaDoPortaMalas, PortaTraseiraDoMotor,Traseira, +Dianteira,FaróisDianteiros,FaroisTraseiros,CurrentPic,CurrentPicPath)价值 +(@ Param1,@ Param2,@ Param3,''OK'',''OK'' ,''确定',''确定'',''确定'',''确定'',''确定'',''确定'',''确定'',''确定'', +''确定'',' OK'',''OK'',''OK'',''OK'',''OK'',''OK'',''OK'',''OK'',''OK' ',''确定'',''确定'',''确定'',''确定'', +''确定'',''确定'',''确定''' ,''确定'',NULL,NULL); cmd.Parameters.Add(@ Param1,SqlDbType.Char).Value = noPlateTextBox.Text; cmd.Parameters.Add(@ Param2,SqlDbType.Char).Value = licenseDateTimePicker.Text; cmd.Parameters.Add(@ Param3,SqlDbType.Char).Value = controlDateTimePicker.Text; cmd.Connection = conn; conn.Open(); intInsert = cmd.ExecuteNonQuery(); conn.Close(); if(intInsert!= 0) { MessageBox.Show(数据插入成功,成功,MessageBoxButtons.OK, MessageBoxIcon。信息); noPlateTextBox.Text =; okButton.Enabled = false; Main_Load.bindComboBox(); //这里是需要的电话<<<<< br mode =hold/> } else { MessageBox.Show(数据插入失败,错误,MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); } } 好的,我明白了。我发现它在互联网上的某个地方。它完成了这项工作! 主要m =(主要)Application.OpenForms [ Main]; m.bindComboBox(); wolfcoder75,谢谢你的输入。 Hi!this is my code to polulate a combobox with data from the database.Main_Load(object sender, EventArgs e) { bindComboBox(); } public void bindComboBox() { DataTable table = new DataTable(); conn.ConnectionString = connString; conn.Open(); SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM KombiStateTbl", conn); da.Fill(table); kombiComboBox.DataSource = new BindingSource(table, null); kombiComboBox.DisplayMember = "NumberPlate"; kombiComboBox.ValueMember = "NumberPlate"; conn.Close(); }from another form I can add another record to the database. my question is: how to update the combobox so it shows that record right after it''s added to the db? this is the code to add a record:private void okButton_Click(object sender, EventArgs e) { int intInsert = 0; String connString = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Projects\RentalPlus\RentalPlus\KombiDB.mdf;Integrated Security=True;User Instance=True"); SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "INSERT INTO KombiStateTbl (NumberPlate, LicenseExpiryDate, ControlDate, " + "Documentos, FaróisLanternas, EstepeMacacoChaveDeRoda, Triângulo, Extintor, Acendedor, " + "RodasCalota, Retrovisores, Párabrisa, BancosTapetes, Pneus, ParachoqueDianteiro, " + "ParachoqueTraseiro, PortaDianteiraDireitaM, PortaDianteiraEsquerdaP, PortaLateral, " + "LateralEsquerda, LateralEsquerdaTraseira, LateralDireitaTraseira, ParalamaTraseiroDireito, " + "ParalamaTraseiroEsquerdo, Teto, PortaDoPortaMalas, PortaTraseiraDoMotor, Traseira, " + "Dianteira, FaróisDianteiros, FaroisTraseiros, CurrentPic, CurrentPicPath) VALUES " + "(@Param1,@Param2,@Param3, 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', " + "'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', " + "'OK', 'OK', 'OK', 'OK', NULL, NULL)"; cmd.Parameters.Add("@Param1", SqlDbType.Char).Value = noPlateTextBox.Text; cmd.Parameters.Add("@Param2", SqlDbType.Char).Value = licenseDateTimePicker.Text; cmd.Parameters.Add("@Param3", SqlDbType.Char).Value = controlDateTimePicker.Text; cmd.Connection = conn; conn.Open(); intInsert = cmd.ExecuteNonQuery(); conn.Close(); if (intInsert != 0) { MessageBox.Show("The data insertion is successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); noPlateTextBox.Text = ""; okButton.Enabled = false; } else { MessageBox.Show("The data insertion is failed", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); } }Please help. 解决方案 You need to do a reload to recollect the updated content and then update the datasource of the combo box after you added that content.this in your if statement should fix that:if (intInsert != 0){ MessageBox.Show("The data insertion is successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); noPlateTextBox.Text = ""; okButton.Enabled = false; bindComboBox(); //<<- Recollect content for combobox after update.}sorry, my fault on the only calling the bind line above.Just add the call to your if condition inside your ok button click after ok_Button.Enabled = false;So your okButton_Click would look like this:private void okButton_Click(object sender, EventArgs e) { int intInsert = 0; String connString = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Projects\RentalPlus\RentalPlus\KombiDB.mdf;Integrated Security=True;User Instance=True"); SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "INSERT INTO KombiStateTbl (NumberPlate, LicenseExpiryDate, ControlDate, " + "Documentos, FaróisLanternas, EstepeMacacoChaveDeRoda, Triângulo, Extintor, Acendedor, " + "RodasCalota, Retrovisores, Párabrisa, BancosTapetes, Pneus, ParachoqueDianteiro, " + "ParachoqueTraseiro, PortaDianteiraDireitaM, PortaDianteiraEsquerdaP, PortaLateral, " + "LateralEsquerda, LateralEsquerdaTraseira, LateralDireitaTraseira, ParalamaTraseiroDireito, " + "ParalamaTraseiroEsquerdo, Teto, PortaDoPortaMalas, PortaTraseiraDoMotor, Traseira, " + "Dianteira, FaróisDianteiros, FaroisTraseiros, CurrentPic, CurrentPicPath) VALUES " + "(@Param1,@Param2,@Param3, ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', " + "''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', ''OK'', " + "''OK'', ''OK'', ''OK'', ''OK'', NULL, NULL)"; cmd.Parameters.Add("@Param1", SqlDbType.Char).Value = noPlateTextBox.Text; cmd.Parameters.Add("@Param2", SqlDbType.Char).Value = licenseDateTimePicker.Text; cmd.Parameters.Add("@Param3", SqlDbType.Char).Value = controlDateTimePicker.Text; cmd.Connection = conn; conn.Open(); intInsert = cmd.ExecuteNonQuery(); conn.Close(); if (intInsert != 0) { MessageBox.Show("The data insertion is successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); noPlateTextBox.Text = ""; okButton.Enabled = false; Main_Load.bindComboBox(); //here is the call needed <<<<br mode="hold" /> } else { MessageBox.Show("The data insertion is failed", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); } }Ok, i got it. i found it somewhere on the internet. it did the job! Main m = (Main)Application.OpenForms["Main"]; m.bindComboBox();wolfcoder75, thank you for yr input. 这篇关于如何更新组合框,以便在将其添加到数据库后立即显示该记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-31 17:02