C# 刷新列表框';s数据源
我有一个用于更新方法的表单,该表单位于详细视图中。在文本框旁边有一个列表框,显示数据库表中所有名称的名称。在列表框下,我还有一个额外的文本框,用于快速搜索名称,以防用户想要输入它 当我去更新其中一个名称时,例如将John更改为Jonathan,数据库将使用我在sql server上选中的新名称进行更新,但列表框中的名称不会更改!通过将列表框当前选中的位置移动到movefirst(),有一种肮脏的方法可以解决此问题。然而,在列表框下我有一个文本框,正如我所提到的,它是一个快速搜索,所以我在搜索文本框中键入Jonathan,但没有显示任何内容。但是,如果我键入以前的名称John,那么我将在表中获得此行的详细信息 有没有办法解决这个问题 更新1: 我尝试过将listbox数据源设为空,然后重新分配,但没有成功。我把我的更新表代码放在下面C# 刷新列表框';s数据源,c#,refresh,datasource,C#,Refresh,Datasource,我有一个用于更新方法的表单,该表单位于详细视图中。在文本框旁边有一个列表框,显示数据库表中所有名称的名称。在列表框下,我还有一个额外的文本框,用于快速搜索名称,以防用户想要输入它 当我去更新其中一个名称时,例如将John更改为Jonathan,数据库将使用我在sql server上选中的新名称进行更新,但列表框中的名称不会更改!通过将列表框当前选中的位置移动到movefirst(),有一种肮脏的方法可以解决此问题。然而,在列表框下我有一个文本框,正如我所提到的,它是一个快速搜索,所以我在搜索文本
命名空间Windows窗体应用程序1 { 公共部分类updateContact:表单 { public updateContact() { 初始化组件(); }
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)
{
}
}
}更新源代码后,您必须再次设置列表框的
数据源
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)
{
}
}
如下所示:这是我的数据:
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"},
};
}
对于更新,如下所示:
private void button1_Click(object sender, EventArgs e)
{
MyDataSource.Persons[0].Age = 45;
listBox1.DataSource = null;
listBox1.DataSource = MyDataSource.Persons;
listBox1.DisplayMember = "Age";
}
这只是一个根据需要更改代码的示例。更新源代码后,您必须再次设置列表框的数据源
如下所示:这是我的数据:
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"},
};
}
对于更新,如下所示:
private void button1_Click(object sender, EventArgs e)
{
MyDataSource.Persons[0].Age = 45;
listBox1.DataSource = null;
listBox1.DataSource = MyDataSource.Persons;
listBox1.DisplayMember = "Age";
}
这只是一个根据需要更改代码的示例。如果数据源是DataTable,则只需调用AcceptChanges(),如下所示:
listBox.DataSource = null;
((DataTable)listBox.DataSource).AcceptChanges();
如果数据源是DataTable,则只需调用AcceptChanges(),如下所示:
listBox.DataSource = null;
((DataTable)listBox.DataSource).AcceptChanges();
不知道为什么你为数据库创建了一个person对象类,这是一个你应该做的练习吗?即使我已经了解了OOP。但这是我第一次独自做一个项目来学习数据库。例如,我的数据库中有一个contacts表,我应该创建一个contacts类吗?并列出所有成员该如何创建s链接到我的sql数据库联系表tho?这只是一个想象虚拟数据库的示例,取决于您的需要,有许多技术可以自动解决这个问题,称为ORM(对象关系映射),实体框架是其中之一。我不知道为什么你要为数据库创建一个person对象类,这是一个你应该做的练习吗?即使我已经了解了OOP。但这是我第一次独自做一个项目来学习数据库。例如,我的数据库中有一个contacts表,我应该创建一个contacts类吗l成员们,这个类将如何链接到我的sql数据库联系表tho?这只是一个想象虚拟数据库的示例,取决于您的需要,有许多技术可以自动解决这个问题,称为ORM(对象关系映射),实体框架就是其中之一