-2

I want to get values from a selected row of DataGridView and assign them into a few different controls on a Form when a control's name match a column's name (there is TextBox, ComboBox and NumericUpDown).

This is how I am populating the controls currently:

 Form myForm = new Form();
 if (comboBoxTable.Text == "Client")
{
       myForm = new EditClientDataWindow();
 }
else if (comboBoxTable.Text == "Agency")
 {
       myForm = new EditAgencyDataWindow();
 }
else if (comboBoxTable.Text == "Medicine")
 {
       myForm = new EditMedicineDataWindow();
 }
string val;
foreach (Control c in myForm.Controls)
{
    for (int i = 0; i < dataGridViewMDB.ColumnCount; i++)
    {
          val = dataGridViewMDB.SelectedRows[0].Cells[i].Value.ToString();
          if (dataGridViewMDB.Columns[i].Name.ToString() ==
                            c.Name.ToLower().Replace(c.GetType().ToString().ToLower().Replace("system.windows.forms.", ""), ""))
          {
          if (c is NumericUpDown)
               (c as NumericUpDown).Value = Convert.ToInt32(val);
          else
               c.Text = val;
          }
    }
}

With the exception of NumericUpDown, the other types of controls are correctly populated. For the NumericUpDown, I only ever get the default -1 value. I've also tried to use decimal.Parse() and Convert.ToDecimal() instead of Convert.ToInt32(), but there is no change in the result.

The range of NumericUpDown had been set to -1 and 999.

4

1 回答 1

0

这不是一个答案,而是一种减少错误搜索空间的方法,因为它是在幕后发生的很多事情。

你能把你的代码改成

foreach (Control c in myForm.Controls)
{
    if (c is NumericUpDown)
       (c as NumericUpDown).Value = 12;
    else
       c.Text = "12";
}

在此之后,所有控件都应设置为 12。请您对其进行测试并返回观察结果。

于 2013-06-16T09:54:10.587 回答