从combobox将字符串转换为double

时间:2014-05-01 17:06:05

标签: c# combobox selectedvalue

我很难获得从组合框返回的字符串以转换为double。我在网上做了一些研究,我相信它应该有效。我不断收到用户异常。

具体来说,我的代码的以下部分存在问题:

    private void cboBeverage_SelectedIndexChanged(object sender, EventArgs e)
    {

            string tempString = cboBeverage.SelectedValue.ToString();
            double tempPrice = Convert.ToDouble(tempString);
            Calculations(tempPrice);

    }

这是我的整个代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JO_BillCalculator
{
    public partial class Form1 : Form
    {
        double subtotal = 0.00;
        double tax = 6.875;
        double total = 0.00;

        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Add data to dtBeverage datatable
            DataTable dtBeverage = new DataTable();

            DataColumn dc1Beverage = new DataColumn("ItemBeverage");
            DataColumn dc2Beverage = new DataColumn("PriceBeverage");

            dtBeverage.Columns.Add(dc1Beverage);
            dtBeverage.Columns.Add(dc2Beverage);

            dtBeverage.Rows.Add("", "0.00");
            dtBeverage.Rows.Add("Milk", "1.50");
            dtBeverage.Rows.Add("Juice", "2.50");
            dtBeverage.Rows.Add("Mineral Water", "2.95");
            dtBeverage.Rows.Add("Coffee", "1.25");
            dtBeverage.Rows.Add("Tea", "1.50");
            dtBeverage.Rows.Add("Soda", "1.95");

            //Display data in correct combobox
            cboBeverage.DataSource = dtBeverage;
            cboBeverage.DisplayMember = "ItemBeverage";
            cboBeverage.ValueMember = "PriceBeverage";

            //Add data to dtAppitizer datatable
            DataTable dtAppitizer = new DataTable();

            DataColumn dc1Appitizer = new DataColumn("ItemAppitizer");
            DataColumn dc2Appitizer = new DataColumn("PriceAppitizer");

            dtAppitizer.Columns.Add(dc1Appitizer);
            dtAppitizer.Columns.Add(dc2Appitizer);
            dtAppitizer.Rows.Add("", "0.00");
            dtAppitizer.Rows.Add("Buffalo Wings", "5.95");
            dtAppitizer.Rows.Add("Buffalo Fingers", "6.95");
            dtAppitizer.Rows.Add("Potato Skins", "8.95");
            dtAppitizer.Rows.Add("Nachos", "8.95");
            dtAppitizer.Rows.Add("Mushroom Caps", "10.95");
            dtAppitizer.Rows.Add("Shrimp Cocktail", "12.95");
            dtAppitizer.Rows.Add("Chips and Salsa", "6.95");

            //Display data in correct combobox
            cboAppetizer.DataSource = dtAppitizer;
            cboAppetizer.DisplayMember = "ItemAppitizer";
            cboAppetizer.ValueMember = "PriceAppitizer";

            //Add data to dtMainCourse datatable
            DataTable dtMainCourse = new DataTable();

            DataColumn dc1MainCourse = new DataColumn("ItemMainCourse");
            DataColumn dc2MainCourse = new DataColumn("PriceMainCourse");

            dtMainCourse.Columns.Add(dc1MainCourse);
            dtMainCourse.Columns.Add(dc2MainCourse);

            dtMainCourse.Rows.Add("", "0.00");
            dtMainCourse.Rows.Add("Chicken Alfredo", "13.95");
            dtMainCourse.Rows.Add("Chicken Picatta", "13.95");
            dtMainCourse.Rows.Add("Turkey Club", "11.95");
            dtMainCourse.Rows.Add("Lobster Pie", "19.95");
            dtMainCourse.Rows.Add("Prime Rib", "20.95");
            dtMainCourse.Rows.Add("Shrimp Scampi", "18.95");
            dtMainCourse.Rows.Add("Turkey Dinner", "13.95");
            dtMainCourse.Rows.Add("Stuffed Chicken", "14.95");
            dtMainCourse.Rows.Add("Seafood Alfredo", "15.95");

            //Display data in correct combobox
            cboMainCourse.DataSource = dtMainCourse;
            cboMainCourse.DisplayMember = "ItemMainCourse";
            cboMainCourse.ValueMember = "PriceMainCourse";

            //Add data to dtDessert datatable
            DataTable dtDessert = new DataTable();

            DataColumn dc1Dessert = new DataColumn("ItemDessert");
            DataColumn dc2Dessert = new DataColumn("PriceDessert");

            dtDessert.Columns.Add(dc1Dessert);
            dtDessert.Columns.Add(dc2Dessert);

            dtDessert.Rows.Add("", "0.00");
            dtDessert.Rows.Add("Apple Pie", "5.95");
            dtDessert.Rows.Add("Sundae", "3.95");
            dtDessert.Rows.Add("Carrot Cake", "5.95");
            dtDessert.Rows.Add("Mud Pie", "4.95");
            dtDessert.Rows.Add("Apple Crisp", "5.95");

            //Display data in correct combobox
            cboDessert.DataSource = dtDessert;
            cboDessert.DisplayMember = "ItemDessert";
            cboDessert.ValueMember = "PriceDessert";




        }

        private void cboBeverage_SelectedIndexChanged(object sender, EventArgs e)
        {

                string tempString = cboBeverage.SelectedValue.ToString();
                double tempPrice = Convert.ToDouble(tempString);
                Calculations(tempPrice);

        }

        private void cboAppetizer_SelectedIndexChanged(object sender, EventArgs e)
        {

            cboAppetizer.SelectedIndex = 0;
        }

        private void cboMainCourse_SelectedIndexChanged(object sender, EventArgs e)
        {

            cboMainCourse.SelectedIndex = 0;
        }

        private void cboDessert_SelectedIndexChanged(object sender, EventArgs e)
        {

            cboDessert.SelectedIndex = 0;
        }

        private void Calculations(double price)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:2)

使用selectedItem而不是SelectedValue

DataRow selectedDataRow = ((DataRowView)cboBeverage.SelectedItem).Row;
double tempPrice = Convert.ToDouble(selectedDataRow["PriceBeverage"]);
Calculations(tempPrice);