没有张贴我的代码全部(我只是张贴违规脚本),我有这似乎是很多人都有过一个问题,但雷似乎没有其他的建议更新回应。我在VS2008中使用C#。C#组合框的SelectedValue空
基本上我有一个组合框,并且当物品的init改变它转到下面的代码。本质上,代码将确定选择哪个国家(myCountryKey),然后将其作为参数传递给填充后续组合框的存储过程。
什么奇怪的是,cboCountries的了selectedValue PROPERT总是显示为空。在阅读这个问题时,看起来好像dropdownStyle属性是问题,但是我按照建议将其更改为DropDownList,并且不起作用。
因为我使用dropdowns很多,我开始玩arond,发现我可以让SelectedIndex属性工作,我也可以使用GetItemText属性来工作(这是myCountryKey2和myCountryKey3变量的用途)。然而,我真正想要的是SelectedValue,之前我已经完成了这些工作,并且无法理解它为什么不起作用。
是否有任何其他组合框属性,我可能不小心改变了可能会犯的SelectedValue不工作?
private void cboCountries_SelectedIndexChanged(object sender, EventArgs e)
{
SqlCommand cmd = null;
SqlDataReader dr = null;
try
{
cmd = util.SqlConn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
myCountryKey = int.Parse(this.cboCountries.SelectedValue.ToString()); //does not work, says value is null
myCountryKey2 = int.Parse(this.cboCountries.SelectedIndex.ToString()); //Works fine
string myCountryKey3 = this.cboCountries.GetItemText(this.cboCountries.SelectedItem).ToString(); //Works fine
cboDivisions.Enabled = true;
cboDivisions.Items.Clear();
// Division parameter
cmd.CommandText = "proc_parms_division";
dr = cmd.ExecuteReader();
while (dr.Read())
this.cboDivisions.Items.Add(new ListItem(dr["division_name"].ToString(), dr["division_key"].ToString()));
dr.Close();
cmd.Parameters.Clear();
}
catch (Exception ex)
{
util.LogError(ex);
MessageBox.Show(ex.Message);
}
finally
{
if (dr != null) dr.Dispose();
if (cmd != null) cmd.Dispose();
}
}
更多的代码:
public frmWriteoff()
{
InitializeComponent();
//this.KeyPreview = true;
//this.KeyUp += new KeyEventHandler(frmWriteoff_KeyUp);
// Data objects are unmanaged code.
// Declare them above the try{} block and always dispose of them in finally{}.
SqlCommand cmd = null;
SqlDataReader dr = null;
try
{
this.cboCountries.DisplayMember = "Label";
this.cboCountries.ValueMember = "Value";
this.cboDivisions.DisplayMember = "Label";
this.cboDivisions.ValueMember = "Value";
this.cboBusinessUnits.DisplayMember = "Label";
this.cboBusinessUnits.ValueMember = "Value";
this.cboMCCs.DisplayMember = "Label";
this.cboMCCs.ValueMember = "Value";
this.cboNodes.DisplayMember = "Label";
this.cboNodes.ValueMember = "Value";
this.cboProductCodes.DisplayMember = "Label";
this.cboProductCodes.ValueMember = "Value";
// Country parameters
cmd = util.SqlConn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "proc_parms_countries_for_user";
cmd.Parameters.Add("@whoAmI", SqlDbType.VarChar).Value = WindowsIdentity.GetCurrent().Name;
dr = cmd.ExecuteReader();
while (dr.Read())
if (dr["country key"].ToString() != "0" && dr["country key"].ToString() != "1")
{
this.cboCountries.Items.Add(new ListItem(dr["country name"].ToString(), dr["country key"].ToString()));
}
cmd.Parameters.Clear();
dr.Close();
}
catch (Exception ex)
{
util.LogError(ex);
MessageBox.Show(ex.Message);
}
finally
{
if (dr != null) dr.Dispose();
if (cmd != null) cmd.Dispose();
}
}
你实际上已经设置有冲突的组合框的Value属性的结合,或添加新的ListItem它时提供的价值? –
James,我解开了上面的代码,以便您可以看到我的ValueMember设置,我认为这是您要求的。还包括cboCountries组合框的初始加载代码。谢谢。 –