【问题标题】:C# - getting value of SelectedItem in Windows Forms applicationC# - 在 Windows 窗体应用程序中获取 SelectedItem 的值
【发布时间】:2012-04-25 15:21:56
【问题描述】:

我有一个简单的 Windows 窗体应用程序(带有一个 Access 数据库),它带有一个组合框 (cmbStores),它以可以想象的最简单的方式填充。

问题:我无法获取所选项目的值。

这是我填充此组合框的方式:

// Variable declaration
        string strQueryStores = "SELECT StoreNumber FROM tblStoresAndRegion ORDER BY StoreNumber";
        string strConnectionString = UtilityClass.GetConnectionString();
        OleDbConnection connStores;
        OleDbDataReader readerStores = null;

        connStores = new OleDbConnection(strConnectionString);

        try
        {
            connStores.Open();
            OleDbCommand sqlGetStores = new OleDbCommand(strQueryStores, connStores);

            cmbStore.Items.Clear();
            cmbStore.Items.Add("All");
            if (connStores != null)
            {
                readerStores = sqlGetStores.ExecuteReader();

                if (readerStores.HasRows)
                {
                    while (readerStores.Read())
                    {
                        cmbStore.Items.Add (Convert.ToInt32(readerStores["StoreNumber"]));
                    }
                }
            }
            cmbStore.SelectedIndex = 0;

        }

        catch (OleDbException oledblEX)
        {
            MessageBox.Show(oledblEX.Message);
        }

        finally
        {
            if (readerStores != null)
                readerStores.Close();
            if (connStores != null)
                connStores.Close();
        }

这就是我试图获取所选项目的价值的方式。

int nStoreNumber = Convert.ToInt32(cmbABSM.SelectedItem);

【问题讨论】:

  • 您用于获取所选项目的代码在哪里?你有例外吗?值不正确?当您尝试获取所选项目时发生了什么?
  • 您的代码有些不一致。您填写 cmdStore 框,并尝试读取 cmbABSM 框。真的是这样吗,还是你打错了?
  • @Matzi ...我在这里打错了。代码应该是 int nStoreNumber = Convert.ToInt32(cmbStore.SelectedItem);
  • 已解决(但仍然很困惑)...当我使用此代码时,int nStoreNumber = Convert.ToInt32(cmbStore.Text);它有效。

标签: c# combobox selecteditem


【解决方案1】:

我知道我来晚了,但这很好用:

int? nStoreNumber = cmbABSM.SelectedValue as int?;
if (nStoreNumber==null)
    return;

【讨论】:

    【解决方案2】:

    如果为组合框设置了ValueMember,请尝试使用SelectedValue,否则默认使用Text 属性:

    //If ValueMember is set
    int nStoreNumber = Convert.ToInt32(cmbABSM.SelectedValue);
    
    //Otherwise
    int nStoreNumber = Convert.ToInt32(cmbABSM.Text);
    

    无论哪种方式,我都建议您确保所选内容的值是有效的int

    int nStoreNumber;
    
    if (!int.TryParse(cmbABSM.SelectedValue, out nStoreNumber))
    {
        //This is not a valid number.  Notify the user.
    }
    

    【讨论】:

      【解决方案3】:

      Int32.Parse(box.SelectedItem.ToString());
      

      为你工作?

      【讨论】:

      • 您应该真正使用该项目的ValueText 属性。读者不清楚ToString返回的是哪个。
      【解决方案4】:

      您可以使用 SelectedItem.Value 或 SelectedValue,实际区别在于它们在没有选择时返回的内容。

      SelectedItem.Value 返回值,如果没有选中项返回null。

      SelectedValue 也返回值,但是如果没有选中的项会返回一个空字符串

      进一步阅读:

      http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selecteditem.aspx

      http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue.aspx

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-13
        • 1970-01-01
        相关资源
        最近更新 更多