组合框与多个表的数学运算值

时间:2013-07-19 15:30:16

标签: c# sql sql-server combobox

我帮你们一些人创造了这个:

 spojeni.Open();
 var cb4 = new SqlCommand("SELECT cena1,cena2,cena3,tcena1,tcena2,tcena3 FROM zajezd WHERE akce="+zakce.Text,spojeni);

 SqlDataReader dr4 = cb4.ExecuteReader();
            while (dr4.Read())

            {
                string val1 = Convert.ToString(dr4["cena1"]);
                string val2 = Convert.ToString(dr4["cena2"]);
                string val3 = Convert.ToString(dr4["cena3"]);
                string tval1 = Convert.ToString(dr4["tcena1"]);
                string tval2 = Convert.ToString(dr4["tcena2"]);
                string tval3 = Convert.ToString(dr4["tcena3"]);
                comboBox4.Items.Add(val1 + " -- " + tval1);
                comboBox4.Items.Add(val2 + " -- " + tval2);
                comboBox4.Items.Add(val3 + " -- " + tval3);

            }
            dr4.Close();
            dr4.Dispose();

            spojeni.Close();

现在让我的问题更加明确。 val1,val2,va3是numeric(9.2)值,tcena1,tcena2,tcena3是char(10)。我有textbox1,其中我插入的数字像4或其他什么。我希望它从val1,val2,val3中选择,具体取决于SelectedItemcomboBox4的值comboBox4。要将textBox1中的选定值与textBox2值相乘。

最终值将显示在numeric(9,2)中。 所以我想问你如何从val1,val2或val3中选择comboBox4值,具体取决于 try { // textBox19.Text = (Convert.ToDouble(comboBox4.SelectedValue) * Convert.ToInt32(textBox16.Text.Trim())).ToString(); if (comboBox4.SelectedValue != null) { int textboxValue = 0; double comboxValue = 0; if (double.TryParse(comboBox4.SelectedValue.ToString(), out comboxValue) && int.TryParse(textBox16.Text.Trim(), out textboxValue)) { textBox19.Text = (comboxValue * textboxValue).ToString(); } } } catch (Exception ex) { MessageBox.Show("Chybové hlášení2: " + ex.Message.ToString()); } 中选择的项目并乘以?提前致谢

我们应该如何改进它?提前谢谢。

{{1}}

enter image description here

1 个答案:

答案 0 :(得分:2)

你正在寻找这样的东西。

if(comboBox1.SelectedValue != null)
{
    int textboxValue = 0;
    double comboxValue = 0;
     if(double.TryParse(comboBox1.SelectedValue.ToString(),out comboxValue) && int.TryParse(text1.Text.Trim(),out textboxValue))

      {
        textbox2.Text = (comboxValue * textboxValue).ToString();
      }

}