我对winforms组合框感到奇怪。
注意:这不是数据绑定 - 我从数据表中填充所有控件。
我将一个帐号列表添加到组合框中,然后在网格中显示一组记录在组合框文本值上的记录。
我可以添加帐号,在帐号之间切换,一切正常。
如果我从框中删除了一个帐号,我会从数据库中删除帐号和所有相关记录。这很好。
然后,我从accounts表中的剩余值重新填充组合框,并将SelectedIndex设置为0,以便将其设置为列表中的第一个帐号。
if(cboAccountNumbers.Items.Count > 0)
{
cboIBAccountNumbers.SelectedIndex = 0;
}
然后我调用db来获取在combobox.Text值上键入的记录 - 这是有效的 - 有一个文本值,它是正确的,并返回数据。
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();
}
}