3

没有张贴我的代码全部(我只是张贴违规脚本),我有这似乎是很多人都有过一个问题,但雷似乎没有其他的建议更新回应。我在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(); 
      } 
     } 
+0

你实际上已经设置有冲突的组合框的Value属性的结合,或添加新的ListItem它时提供的价值? –

+0

James,我解开了上面的代码,以便您可以看到我的ValueMember设置,我认为这是您要求的。还包括cboCountries组合框的初始加载代码。谢谢。 –

回答

0

为了利用comboBox.SelectedValue,你必须先设置comboBox.ValueMember

ValueMember (MSDN)

MSDN上的例子演示了如何在数据绑定的情况下使用它。

+0

看到我添加的代码,我认为我做对了,但欣赏第二套眼睛。 –

2

我想通了,再绑定数据打击。当我更改我的代码以将我的国家/地区列表加载到数据表中,然后使用该数据表作为我的组合框的源代码时,所有情况都与世界一致。

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(); 

      ///////////////////////////////////////////////////////////////////////////////////////////////// 

      var dtCountries = new DataTable(); 
      dtCountries.Columns.Add("Label"); 
      dtCountries.Columns.Add("Value"); 

      //DataRow _countries = dtCountries.NewRow(); 
      //_countries["country key"] = myBusinessUnit; 
      //_countries["country name"] = myDataYear; 

      //dtCountries.Rows.Add(_fcst); 

      while (dr.Read()) 
       if (dr["country key"].ToString() != "0" && dr["country key"].ToString() != "1") 
       { 
        dtCountries.Rows.Add(dr["country name"].ToString(), dr["country key"].ToString()); 
       } 
      cmd.Parameters.Clear(); 
      dr.Close(); 

      this.cboCountries.DataSource = dtCountries; 
+0

沿着与此响应相同的路线,确保在尝试设置SelectedValue时已经发生DataSource绑定。我错误地认为我的Form_Load事件在SelectedValue设置(oops)时已经完成。 –

-1

我理解你的痛苦,请尝试以下

if (myxyzcombo.SelectedItem != null) 
{ 

MessageBox.Show(myxyzcombo.SelectedItem.ToString()); 

} 
+0

替代代码: 如果(!myxyzcombo..SelectedIndex = -1) { MessageBox.Show(myxyzcombo.SelectedItem.ToString()); } –

+2

这不回答这个问题。 – Tunaki

相关问题