[英]How to display primary key in a combobox immediately after insert records to the database c#?
I'm developing a windows form application to insert datagridview values to sqldatabase.I would like to know how to display inserted value's primary key(GRN_No) in a combobox immediately after insert.I tried this for display GRN_No when I load form as well as after insert command. 我正在开发一个Windows窗体应用程序以将datagridview值插入sqldatabase.I想知道如何在插入后立即在组合框中显示插入值的主键(GRN_No)。我在加载窗体以及在插入命令之后。
void fillGRNcombo()
{
DynamicConnection con = new DynamicConnection();
con.mysqlconnection();
con.sqlquery("select GRN_No from TBL_GRN");
con.dataread();
while (con.datareader.Read())
{
cmbGRN.Items.Add((int)con.datareader["GRN_No"]);
}
}
But after insertation values duplicated in combobox like(1,2,1,2,3).How to solve this? 但是插入值重复出现在组合框((1,2,1,2,3))中后,如何解决呢?
In your case your combo box persisting earlier data, that is the reason you can see repeated record in your combo box. 在您的情况下,组合框保留了较早的数据,这就是您可以在组合框中看到重复记录的原因。
Instead of adding same data again and again, just clear combo box before adding new primary keys or new records to it. 不必一遍又一遍地添加相同的数据,只需清除组合框,然后再向其添加新的主键或新记录。
cmbGRN.Items.Clear();
while (con.datareader.Read())
{
cmbGRN.Items.Add((int)con.datareader["GRN_No"]);
}
Simply Clear ComboBox before Reading the DataReader 在读取DataReader之前只需清除ComboBox
cmbGRN.Items.Clear();
while (con.datareader.Read())
{
cmbGRN.Items.Add((int)con.datareader["GRN_No"]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.