Please help with DataGridViev . I need to have a table in 2 columns with the previously specified number of rows ( gg ). In the 1st column there should be numbers in order, and in the second temperature values ​​by the formulas.

My attempt at solving below. In it, when you click the "fill table" button (button 4), the program crashes with the error:

A raw exception of type "System.FormatException" in mscorlib.dll

Additional information: The input string had the wrong format.

Code:

 int gg = Convert.ToInt32(textBox2.Text); double z_pnd = Convert.ToDouble(textBox4.Text), z_pvd = Convert.ToDouble(textBox3.Text), t_kond = Convert.ToDouble(textBox8.Text), t_dear = Convert.ToDouble(textBox9.Text), t_oe = Convert.ToDouble(textBox10.Text), t_egu = Convert.ToDouble(textBox11.Text), z = Convert.ToDouble(textBox2.Text), alfa = 0.9, tp_pereddear = t_dear - 10, t_nach = Convert.ToDouble(textBox6.Text), p_pg = Convert.ToDouble(textBox7.Text), t_pv = ((t_nach - t_kond - t_oe - t_egu) / (z + 1)), t_pvopt = t_nach - t_pv, t_pvek = alfa * (t_pvopt - t_kond) + t_kond, delt_pvd = (t_pvek - t_dear) / z_pvd, delt_pnd = (tp_pereddear - t_kond - t_oe - t_egu) / z_pnd; double[] x = new double[gg]; double[] y = new double[gg]; for (int i = 0; i <= gg - 1; i++) { if (i == 0) { x[i] = 1; y[i] = (t_kond + t_oe + t_egu + t_pv); } else { y[0] = (t_kond + t_oe + t_egu + t_pv); x[i] = (i + 1); y[i] = (y[0] + t_pv*i); } { dataGridView1.Rows[i].Cells[0].Value = x[i].ToString(); dataGridView1.Rows[i].Cells[1].Value = y[i].ToString(); 
  • one
    And what you yourself do not know how to debug? Put a breakpoint at the beginning of the method where the conversion takes place, and go step by step, looking at the values ​​of the variables, and soon you will find where you are crooked. The skill of finding and correcting errors is just as necessary in the profession as writing code. - Bulson
  • @ MikhailK Add to the question the values ​​contained in all the textBoxxxx mentioned in the code - at the time of execution of button4_Click . - Igor

2 answers 2

Add to the question the values ​​contained in all the textBoxxxx mentioned in the code.

Check all lines that should contain numbers:

(Well, you can't do that to people. What is going on in your cycle?)

 private bool CheckInput_Int(TextBox aControl) { int value; return int.TryParse(aControl.Text, out value); } private bool CheckInput_Double(TextBox aControl) { double value; bool result = double.TryParse(aControl.Text, out value); if (!result) MessageBox.Show(string.Format("Could not convert text [{0}] from control {1} to number.", aControl.Text, aControl.Name), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return result; } private void button4_Click(object sender, EventArgs e) { if (!CheckInput_Int(textBox2) || !CheckInput_Double(textBox4) || !CheckInput_Double(textBox3) || !CheckInput_Double(textBox8) || !CheckInput_Double(textBox9) || !CheckInput_Double(textBox10) || !CheckInput_Double(textBox11) || !CheckInput_Double(textBox6) || !CheckInput_Double(textBox7)) { MessageBox.Show("Проверьте значения полей!", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } ... double yBase = t_kond + t_oe + t_egu + t_pv; for (int i = 0; i < gg; i++) { x[i] = i + 1; y[i] = yBase + t_pv * i; ... } 
  • @ MikhailK - and should not have earned. I just made a relative order in the loop, but the cause of the error lies in the TextBox control values, which cannot be converted to numbers. - Igor
  • @ MikhailK Have you heard this expression: "The voice of one crying in the wilderness"? Add to the question the values ​​contained in all textBox ax mentioned in the code - at the time of execution of the button4_Click . - Igor
  • @ MikhailK hmm, good. What value (text, string) is contained in textBox2 when clicking on button4 ? - Igor
  • @ MikhailK You just answer me the last question, otherwise we get a little conversation with the deaf. - Igor
  • @ MikhailK Ok, there has been a progress :). Now list what is contained in the remaining eight text boxes. - Igor

Obviously, some method from Convert cannot convert the string to the required type, since "the string has the wrong format". Notice which value is passed from the TextBox Text property.

If there is a need to specify the format of the string, then use the method receiving instance IFormatProvider , for example: Convert.ToDouble (String, IFormatProvider) .

  • The link in my comment is sample code. In short, you pass the string as an argument for ToDouble (string) which cannot be converted to type double, because it contains invalid characters for this type. - Artyom Okonechnikov