C#组合框SelectedValue Null

时间:2013-06-25 16:44:18

标签: c# visual-studio-2008 combobox selectedvalue selectedindexchanged

如果没有发布我的全部代码(我只是发布有问题的脚本),我遇到的问题似乎很多人都有,但我似乎没有回应其他推荐的修复。我在VS2008中使用C#。

基本上我有一个comboBox,当项目init被更改时,它会转到下面的代码。本质上,代码将确定选择哪个国家(myCountryKey),然后将其作为参数传递给填充后续组合框的存储过程。

奇怪的是,cboCountries的selectedValue属性始终显示为Null。在阅读这个问题时,似乎dropdownStyle属性是问题所在,但我按照建议将我改为DropDownList,但是没有用。

因为我经常使用下拉列表,所以我开始玩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();
            }
        }

3 个答案:

答案 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;

答案 1 :(得分:0)

要使用comboBox.SelectedValue,您必须先设置comboBox.ValueMember

ValueMember (MSDN)

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

答案 2 :(得分:-1)

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

if (myxyzcombo.SelectedItem != null) 
{

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

 }
相关问题