本文介绍了在组合框中选择正确的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的代码中,我在一个组合框中加载一个记录列表。现在我想基于字段label.text更改SelectedValue的静态代码如何实现此代码?
SelectedValue = label.text
In the code below I load a list of records in a combo box. Now I want to change my static code for the SelectedValue based on a field label.text How do I implement this code?
SelectedValue = label.text
private void Land()
{
string queryBLand = "SELECT * FROM Landen";
string BLand = CB_BezoekLand.Text;
using (connection = new SqlConnection(connectionstring))
using (SqlCommand command = new SqlCommand(queryBLand, connection))
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
try
{
connection.Open();
DataTable Bland = new DataTable();
adapter.Fill(Bland);
CB_BezoekLand.DisplayMember = "Land";
CB_BezoekLand.ValueMember = "ID";
CB_BezoekLand.DataSource = Bland;
//HELP CB_BezoekLand.SelectedValue = "161";
CB_FactuurLand.DisplayMember = "Land";
CB_FactuurLand.ValueMember = "ID";
CB_FactuurLand.DataSource = Bland;
}
catch (Exception)
{
CB_BezoekLand.Text = ("-");
MessageBox.Show("Er is een onbekende fout opgetreden. De door u ingevulde gegevens zijn niet verwerkt.", "*** | Error - E111", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
}
推荐答案
CB_BezoekLand.SelectedValue = (int)label.Text;
或更好
or better yet
int idValue;
if (int.TryParse(label.Text, out idValue)) {
CB_BezoekLand.SelectedValue = idValue;
}
这篇关于在组合框中选择正确的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!