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.