我在winforms combobox中遇到了一些奇怪的事情。
注意:这不是数据绑定-我从一个数据表填充所有控件。
我向组合框中添加一个帐号列表,然后在一个网格中显示一组记录,这些记录在组合框文本值上键入。
我可以添加帐号,在帐号之间转换,一切正常。
如果我从框中删除一个帐号,我会从数据库中删除帐号和所有相关的记录。
然后,我用accounts表中的剩余值重新填充组合框,并将SelectedIndex设置为0,以便将其设置为列表中的第一个Account Number。
if(cboAccountNumbers.Items.Count > 0)
{
cboIBAccountNumbers.SelectedIndex = 0;
}
然后我调用数据库来获取combobox上键控的记录。文本值-这是可行的-有一个文本值,它是正确的,数据被返回。
DataTable dt = new DataTable();
dt = DataUtils.GetRowsByAccountNumber(cboAccountNumbers.Text);
但是,当窗体重新获得焦点时,组合框文本为空。
如果我点击下拉菜单,正确的项目在那里-但是,与项目索引0相关联的文本没有显示为组合框文本值。
有什么建议吗?
private void cboAccountNumbers_KeyDown(object sender, KeyEventArgs e)
{
DeleteAccountNumber(sender, e);
}
private void DeleteAccountNumber(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
ComboBox cbo = (ComboBox)sender;
DialogResult result = MessageBox.Show("Are you sure you want to delete this Account?\n\n" +
"All strategies related to the account will be deleted as well.",
"xxx",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Hand);
if (result == DialogResult.Yes)
{
using (PgSQLDb db = new PgSQLDb())
{
// two things to do - delete intersection table values between
// ib_account_numbers and strategies i.e. ib_account_numbers_vs_strategies
// then delete the account number itself
// 1. delete account_numbers_vs_strategies
db.Delete("account_numbers_vs_strategies", " account_number_id = (select id from account_numbers where acc_number ='" + cbo.Text + "')");
// 2. delete account_numbers record
db.Delete("account_numbers", "acc_number = '" + cbo.Text + "'");
}
GetAccounts(string.Empty);
}
}
}
private void GetAccounts(string account)
{
DataTable dt = new DataTable();
dt = DataUtils.GetAccountNumbersTable();
string selectedItem = string.Empty;
cboAccountNumbers.Items.Clear();
foreach (DataRow row in dt.Rows)
{
cboAccountNumbers.Items.Add(row["acc_number"].ToString());
if (Convert.ToBoolean(row["is_default"]))
{
selectedItem = row["acc_number"].ToString();
}
}
// if an AccountNumber is passed in, use that (typically when a
// new account is created)
// else use the default
// else use first item in list.
if (!string.IsNullOrEmpty(account))
{
cboAccountNumbers.SelectedItem = account;
}
else if (!string.IsNullOrEmpty(selectedItem))
{
cboIBAccountNumbers.SelectedItem = selectedItem;
}
else
{
if(cboAccountNumbers.Items.Count > 0)
{
cboAccountNumbers.SelectedIndex = 0;
}
}
GetStrategies();
}
private void GetStrategies()
{
DataTable dt = new DataTable();
string selectedItem = cboAccountNumbers.Text;
dt = DataUtils.GetRowsByAccountNumber(selectedItem);
gvwMain.Rows.Clear();
strategies.Clear(); // list of Strategy objects
foreach (DataRow row in dt.Rows)
{
Strategy cd = new Strategy(Convert.ToInt16(row["strategies_id"]),
row["name"].ToString(),
Convert.ToBoolean(row["active"]),
Convert.ToDecimal(row["percentage_of_portfolio"]),
Convert.ToInt16(row["margin"])
);
strategies.Add(cd);
}
gvwMain.AutoGenerateColumns = false;
BindingList<Strategy> bindingList = new BindingList<Strategy>(strategies);
BindingSource source = new BindingSource(bindingList, null);
gvwMain.DataSource = source;
gvwMain.Refresh();
FormatGrid();
}
}
1条答案
按热度按时间lokaqttq1#
在代码中,您引用了两个不同的组合框:cboAccountNumbers和cboIBAccountNumbers
您的本地字符串'SelectedItem'正在cboIBAccountNumbers上使用,因此cboAccountNumbers将永远不会获得该值,因为它是selecteditem。