1

我的英语不好,因为我是西班牙人,所以如果您有不懂的地方问我,我正在使用翻译。

我的问题是我正在做一个在主窗口中有两个窗口的程序我在第二个窗口中有一个数据网格我用文本框传递信息,问题是通过将总价格乘以我得到甜点的价格甜点,当我通过它时数据网格中的总价格围绕该价格,如果价格发生 1.20,程序将更改为 1。

没有给我任何错误,所以对不起,我将不得不花费整个程序代码。

这是第二个窗口

private void Window_Loaded(object sender, RoutedEventArgs e)
{
   if (Application.Current.Properties["seleccionado"] == null)
   {
       textBox1.IsEnabled = false;
       Postresinfo = new TabladePostre();
   }
   else
   {

        Postresinfo = (TabladePostre) (Application.Current.Properties["seleccionado"]);
        textBox1.IsEnabled=false;
        textBox1.Text = Convert.ToString(Postresinfo.refPostre);
        textBox2.Text = Postresinfo.NombrePostre;
        textBox3.Text = Convert.ToString(Postresinfo.cantidad);
        textBox4.Text = Convert.ToString(Postresinfo.precio);
        textBox5.Text = Convert.ToString(Postresinfo.preciototal);

    }
    LinqdePostresDataContext BasedeDatos;


    string filename = "";
    private void button1_Click(object sender, RoutedEventArgs e)
    {
BasedeDatos(LinqdePostresDataContext)Application.Current.Properties["basedeDatos"];

    Postresinfo.NombrePostre = textBox2.Text;
    Postresinfo.cantidad = Convert.ToInt32(textBox3.Text);
    Postresinfo.precio = Convert.ToDecimal(textBox4.Text);
    Postresinfo.preciototal = Convert.ToDecimal(textBox5.Text);
    Postresinfo.imagen = filename;

    if (Application.Current.Properties["seleccionado"] != null)
    {
       Postresinfo.refPostre=Convert.ToInt32(textBox1.Text);
    }
    else
    {
       BasedeDatos.TabladePostres.InsertOnSubmit(Postresinfo);
    }
        BasedeDatos.SubmitChanges();
        this.Close();

    }
    decimal precio = 0;


    private void button2_Click(object sender, RoutedEventArgs e)
    {
        precio = Convert.ToDecimal(textBox4.Text);
        textBox5.Text = Convert.ToString(precio * Convert.ToDecimal(textBox3.Text));


    }

    private void button9_Click(object sender, RoutedEventArgs e)
    {
        // Configure open file dialog box
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        dlg.FileName = "Document"; // Default file name
        dlg.DefaultExt = ".jpg"; // Default file extension
        dlg.Filter = "Text documents (.jpg)|*.jpg"; // Filter files by extension 

        // Show open file dialog box
        Nullable<bool> result = dlg.ShowDialog();

        // Process open file dialog box results 
        if (result == true)
        {
            // Open document 
            filename = dlg.FileName;
            ImageSourceConverter conversor = new ImageSourceConverter();
            image1.Source = (ImageSource)conversor.ConvertFromString(filename);
        }
    }

这是主窗口:

LinqdePostresDataContext BasedeDatos = new LinqdePostresDataContext();

private void activar(object sender, RoutedEventArgs e)
{

   Cargartabla();
}

private void Cargartabla()
{
   var postre = (from n in BasedeDatos.TabladePostres
                 select n);
    dataGrid1.ItemsSource = postre;
}

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Application.Current.Properties["seleccionado"] = null;
        Ventana2 Ventana2 = new Ventana2();
        Ventana2.Show();
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
      BasedeDatos.TabladePostres.DeleteOnSubmit((TabladePostre)dataGrid1.SelectedItem);
      BasedeDatos.SubmitChanges();
      Cargartabla();
    }

    private void Activar2(object sender, EventArgs e)
    {
        Cargartabla();
    }

    private void button3_Click(object sender, RoutedEventArgs e)
    {
   Application.Current.Properties["seleccionado((TabladePostre)dataGrid1.SelectedItem);
        Application.Current.Properties["basedeDatos"] = BasedeDatos;
        Ventana2 ventana2 = new Ventana2();
        ventana2.Show();
    }

如果您还需要知道我有一个带有 int 价格的数据库,总价格中的 int 数量以十进制表示。

感谢您的回复,我尝试了两个选项都给了我,但都不起作用,这些是故障:

Postresinfo.refPostre = float.Parse (textBox1.Text) 没有给我任何错误,程序运行正常,没有任何变化

float.TryParse (textBox1.Text, out Postresinfo.refPostre) 有这些故障:

错误 1 ​​属性、索引器或动态成员访问不能作为 out 或 ref 参数传递。

错误 2 'float.TryParse (string, out float)' 的最佳重载方法匹配有一些无效参数

错误 3 参数 2:无法从“out int”转换为“float out”

我尝试了其他代码,没有任何变化,程序运行正常

Shane 现在,我尝试了以下代码:

Convert.ToDecimal (textBox1.Text);

但它没有任何改变并正常运行。

我也尝试了其他代码,但没有任何变化,程序运行正常

错误不是textbox1,我认为这是因为它在那个TextBox5文本框中,我以十进制输入价格并将其传递给数据网格并且是四舍五入的

我还将附上列失败的我:

<DataGridTextColumn Binding="{Binding Path=preciototal}" Header="Precio Total"/>

数据网格

谢谢。

4

2 回答 2

0

您的问题是您正在将“1.20”之类的价格转换为带有Convert.ToInt32(textBox1.Text);.

尝试更改Postresinfo.refPostre为小数并使用Convert.ToDecimal(textBox1.Text);这样的东西,因为FormatException可以抛出:

try
{
    Convert.ToDecimal(textBox1.Text);
}
catch (Exception)
{
    //Deal with Error
}
于 2013-08-19T11:53:21.603 回答
0

你这里有很多代码,我不确定是什么问题。我知道您获得了整数值,但提及它发生在其中的文本框的名称会很有帮助。

我确实看到了这段代码:

Postresinfo.refPostre=Convert.ToInt32(textBox1.Text);

如果in的值是浮点数,button1_Click其中将导致舍入。textBox1.Text

你应该做

Postresinfo.refPostre=float.Parse(textBox1.Text);

或者

float.TryParse(textBox1.Text, out Postresinfo.refPostre);

因为它是一个文本框,你可能会得到一个不是数字的值。如果您决定使用,Parse那么您应该这样做

try
{
   Postresinfo.refPostre=float.Parse(textBox1.Text);
}
catch
{
   // Show a message or write to log or simething
}
于 2013-08-19T11:53:26.567 回答