0

我的更新方法有一个表单,该表单是详细视图。在文本框旁边,我有一个列表框,其中显示了数据库表中所有名称的名称。在列表框下,我还有一个额外的文本框可以快速搜索名称,以防用户想要输入它。

当我去更新其中一个名称时,例如将 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)
    {

    }
}

}

4

2 回答 2

2

更新源后,您将DataSource不得不再设置一次列表框。

如下所示:这是我的数据:

 public class Person
    {
        public int Age { get; set; }
        public string Name { get; set; }
    }

    public class MyDataSource
    {
        public static List<Person> Persons = new List<Person>
        {
            new Person{Age=30,Name="Ram"},
            new Person{Age=33,Name="Rahim"},
        };
    }

然后在表单的构造函数中,您可以执行以下操作:

 listBox1.DataSource = MyDataSource.Persons;
 listBox1.DisplayMember = "Age";

然后进行更新,如下所示:

private void button1_Click(object sender, EventArgs e)
        {
            MyDataSource.Persons[0].Age = 45;
            listBox1.DataSource = null;
            listBox1.DataSource = MyDataSource.Persons;
            listBox1.DisplayMember = "Age";
        }

这只是根据您的需要更改代码的示例。

于 2012-07-27T20:19:42.320 回答
1

如果您的 DataSource 是一个 DataTable,您所要做的就是调用 AcceptChanges(),如下所示:

listBox.DataSource = null;  
((DataTable)listBox.DataSource).AcceptChanges();  
于 2015-09-16T17:02:06.743 回答