我的更新方法有一个表单,该表单是详细视图。在文本框旁边,我有一个列表框,其中显示了数据库表中所有名称的名称。在列表框下,我还有一个额外的文本框可以快速搜索名称,以防用户想要输入它。
当我去更新其中一个名称时,例如将 John 更改为 Jonathan,数据库会使用我在 sql server 上检查过的新名称进行更新,但列表框中的名称不会改变!通过将当前选择列表框的位置移动到 movefirst() 来解决此问题有一种肮脏的方法。但是,在列表框下,我有一个文本框,正如我所提到的,它是一个快速搜索,所以我在搜索文本框中输入 Jonathan,但什么也没有出现。但是,如果我输入以前的名字 John,那么我会在表中获得该行的详细信息。
有没有办法解决这个问题?
更新 1:
我尝试使列表框数据源为空,然后再次重新分配它,但它不起作用。我已经把我的更新表格的代码放在下面。
命名空间 WindowsFormsApplication1 { 公共部分类 updateContact :表单 { 公共 updateContact() { InitializeComponent(); }
private void updateContact_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'tblcontactsupdate.tblContacts' table. You can move, or remove it, as needed.
this.tblContactsTableAdapter.Fill(this.tblcontactsupdate.tblContacts);
}
private void btnUpdateContact_Click(object sender, EventArgs e)
{
int x;
Program.da.UpdateCommand = new SqlCommand("Update tblContacts SET FIRSTNAME = @FIRSTNAME, LASTNME = @LASTNME WHERE ID = @ID", Program.cs);
Program.da.UpdateCommand.Parameters.Add("@FIRSTNAME", SqlDbType.VarChar).Value = fIRSTNAMETextBox.Text;
Program.da.UpdateCommand.Parameters.Add("@LASTNME", SqlDbType.VarChar).Value = lASTNMETextBox.Text;
Program.da.UpdateCommand.Parameters.Add("@ID", SqlDbType.VarChar).Value = iDTextBox.Text;
Program.cs.Open();
x = Program.da.UpdateCommand.ExecuteNonQuery();
Program.cs.Close();
if (x >= 1)
{
MessageBox.Show("Record(s) has been updated");
Program.ds.Clear();
Program.da.Fill(Program.ds);
txtfindUpdatecontact.Text = "";
//lbupdateContact.DataSource = null;
//lbupdateContact.DataSource = this.tblcontactsupdate.tblContacts;
}
}
private void txtfindUpdatecontact_TextChanged(object sender, EventArgs e)
{
if (!txtfindUpdatecontact.Text.Equals(""))
{
this.tblContactsBindingSource.Filter = "FIRSTNAME = '" + txtfindUpdatecontact.Text + "'";
}
else
{
this.tblContactsBindingSource.RemoveFilter();
}
}
private void lbupdateContact_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void iDTextBox_TextChanged(object sender, EventArgs e)
{
}
private void fIRSTNAMETextBox_TextChanged(object sender, EventArgs e)
{
}
private void lASTNMETextBox_TextChanged(object sender, EventArgs e)
{
}
}
}