Combobox C在将记录插入sql数据库后更新数据绑定组合框
在我的表单中,我使用组合框从数据库中选择一条记录,然后填充文本框。每当我添加记录或更新时,“删除”组合框都不会获得更新的数据,直到我关闭并重新运行程序。组合框通过设计视图进行数据绑定。基本上,当我更改表中的数据时,我需要这个组合框实时更新。谢谢你的帮助,谢谢 C-VisualStudio2010 代码:Combobox C在将记录插入sql数据库后更新数据绑定组合框,combobox,databound,Combobox,Databound,在我的表单中,我使用组合框从数据库中选择一条记录,然后填充文本框。每当我添加记录或更新时,“删除”组合框都不会获得更新的数据,直到我关闭并重新运行程序。组合框通过设计视图进行数据绑定。基本上,当我更改表中的数据时,我需要这个组合框实时更新。谢谢你的帮助,谢谢 C-VisualStudio2010 代码: 您需要将数据绑定到方法中的combobox,并在插入后立即调用此方法。但是我似乎在你发布的代码中找不到你的数据绑定逻辑…我在表单设计中绑定了组合框,而不是在代码中。你也许应该从代码中进行绑定。或
您需要将数据绑定到方法中的combobox,并在插入后立即调用此方法。但是我似乎在你发布的代码中找不到你的数据绑定逻辑…我在表单设计中绑定了组合框,而不是在代码中。你也许应该从代码中进行绑定。或者至少我不知道如何重新绑定它,如果不是从代码内部绑定的话。我不确定我将如何通过代码、指针来绑定它。嗯,有很多不同的方法。我认为这篇文章将是一个良好的开端:
//Form load
private void Technician_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'technicianDataset.Technician' table. You can move, or remove it, as needed.
this.technicianTableAdapter.Fill(this.technicianDataset.Technician);
}
private void cmo_User_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlCeConnection connection = new SqlCeConnection(@"Data Source=C:\\temp\\Project\\WindowsFormsApplication2\\database.sdf"))
{
try
{
connection.Open();
string sqlText = "SELECT User_ID, Name, Password FROM Technician WHERE User_ID = @User_ID;";
SqlCeCommand command = new SqlCeCommand(sqlText, connection);
command.Parameters.AddWithValue("@User_ID", Convert.ToInt32(cmo_User.SelectedValue));
SqlCeDataReader reader = command.ExecuteReader();
while (reader.Read())
{
txt_userID.Text = reader["User_ID"].ToString();
txt_name.Text = reader["Name"].ToString();
txt_password.Text = reader["Password"].ToString();
}
reader.Close();
connection.Close();
} catch(SqlCeException exp){
Console.Write(exp.ToString());
}
}
}
//Add Record
using (SqlCeConnection connection = new SqlCeConnection(@"DataSource=C:\\temp\\Project\\WindowsFormsApplication2\\OrkneyCheese.sdf"))
{
connection.Open();
string sqlText = "INSERT INTO Technician VALUES (@userid, @name, @password);";
SqlCeCommand command = new SqlCeCommand(sqlText, connection);
command.Parameters.AddWithValue("@name", txt_name.Text);
command.Parameters.AddWithValue("@password", txt_password.Text);
command.Parameters.AddWithValue("@userid", Convert.ToInt32(txt_userID.Text));
command.ExecuteNonQuery();
connection.Close();
cmo_User.Update();
lbl_feedbackTech.Text = "User Successfully added";
}
}