C# Combobox 项目文本更改?
C# Combobox item text change?
我正在制作包含从数据库添加的家庭地址列表的组合框,我正在尝试实现更改组合框中当前地址的文本的选项。例如,它现在是从 db 获取的 addressID,但我想添加一个选项来调用它,例如 "Adress 1" 而不仅仅是 adressID.. 我已经尝试了一些来自之前的建议示例的选项google,但我收到此错误:设置数据源 属性 时无法修改项目集合。 因为我正在使用数据源。有人知道如何解决这个问题吗?
这是我的代码:
private void getAdr()
{
string connStr = "Data Source=MARINCHI\SQLEXPRESS;Initial Catalog=login1;Integrated Security=True";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand getAdr = new SqlCommand("SELECT adressID, userID, adress, floor,city, state, country, zipcode FROM userAdress where userID = @userID", conn);
SqlParameter parUserID = new SqlParameter("@userID", Login.id);
getAdr.Parameters.Add(parUserID);
getAdr.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(getAdr);
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "adressID";
comboBox1.ValueMember = "adressID";
textBox5.DataBindings.Add("Text", dt, "adress");
textBox6.DataBindings.Add("Text", dt, "floor");
textBox7.DataBindings.Add("Text", dt, "city");
textBox8.DataBindings.Add("Text", dt, "state");
textBox9.DataBindings.Add("Text", dt, "country");
textBox10.DataBindings.Add("Text", dt, "zipcode");
comboBox1.Items[comboBox1.SelectedIndex] = "Adress 1";
}
我认为您应该实现组合框的 TextChanged 事件。当用户更改 addressID 时,您应该使用 SqlCommand 更新您的数据库,然后重新加载数据源以刷新组合框项目。
当您设置数据源 属性 时,您必须更改的是 属性 并且您的更改会自动显示在组合中。
为此,我建议您像这样使用 BindingSource 对象:
BindingSource Adresses = new BindingSource();
Adresses.DataSource = dt;
comboBox1.DataSource = Adresses;
comboBox1.DisplayMember = "adressID";
comboBox1.ValueMember = "adressID";
当您更改 dt 对象中的某些数据时,您应该调用:
Adresses.ResetBindings(true);
更新组合的数据。
我正在制作包含从数据库添加的家庭地址列表的组合框,我正在尝试实现更改组合框中当前地址的文本的选项。例如,它现在是从 db 获取的 addressID,但我想添加一个选项来调用它,例如 "Adress 1" 而不仅仅是 adressID.. 我已经尝试了一些来自之前的建议示例的选项google,但我收到此错误:设置数据源 属性 时无法修改项目集合。 因为我正在使用数据源。有人知道如何解决这个问题吗?
这是我的代码:
private void getAdr()
{
string connStr = "Data Source=MARINCHI\SQLEXPRESS;Initial Catalog=login1;Integrated Security=True";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand getAdr = new SqlCommand("SELECT adressID, userID, adress, floor,city, state, country, zipcode FROM userAdress where userID = @userID", conn);
SqlParameter parUserID = new SqlParameter("@userID", Login.id);
getAdr.Parameters.Add(parUserID);
getAdr.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(getAdr);
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "adressID";
comboBox1.ValueMember = "adressID";
textBox5.DataBindings.Add("Text", dt, "adress");
textBox6.DataBindings.Add("Text", dt, "floor");
textBox7.DataBindings.Add("Text", dt, "city");
textBox8.DataBindings.Add("Text", dt, "state");
textBox9.DataBindings.Add("Text", dt, "country");
textBox10.DataBindings.Add("Text", dt, "zipcode");
comboBox1.Items[comboBox1.SelectedIndex] = "Adress 1";
}
我认为您应该实现组合框的 TextChanged 事件。当用户更改 addressID 时,您应该使用 SqlCommand 更新您的数据库,然后重新加载数据源以刷新组合框项目。
当您设置数据源 属性 时,您必须更改的是 属性 并且您的更改会自动显示在组合中。
为此,我建议您像这样使用 BindingSource 对象:
BindingSource Adresses = new BindingSource();
Adresses.DataSource = dt;
comboBox1.DataSource = Adresses;
comboBox1.DisplayMember = "adressID";
comboBox1.ValueMember = "adressID";
当您更改 dt 对象中的某些数据时,您应该调用:
Adresses.ResetBindings(true);
更新组合的数据。