Hi ,
I have a combobox and when clicking on a datagridview row I retrieve the values of the row in the combobox, datatimepicker and other controls on the form.... But the combobox.selecteditem doesn't return the correct value. I wrote messagebox.show(Convert.ToString(comboBox1.selectedItem)) and I saw that it returns System.Data.DataGridView .
I have this line:
comboBox1.SelectedItem =Convert.ToInt32(dataGridView2.SelectedRows[0].Cells[0].Value);
but the combobox's value is not changing when clicking on different rows in the datagridview.
Can you please help me?
Thanks in advance
Loading
VulpesPosted Feb 23, 2012, 11:02 AM
int val = Convert.ToInt32(dataGridView2.SelectedRows[0].Cells[0].Value);
foreach (DataRowView drv in comboBox1.Items)
{
if (Object.Equals(drv[comboBox1.DisplayMember], val))
{
comboBox1.SelectedItem = drv;
break;
}
}
VulpesPosted Feb 24, 2012, 4:53 AM
I thought initially that the problem was that no rows were selected which you could have got around with:
Anyway, no matter if it's solved :)
NelPosted Feb 24, 2012, 2:18 AM
NelPosted Feb 24, 2012, 1:51 AM
"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
in the line:
int val = Convert.ToInt32(dataGridView2.SelectedRows[0].Cells[0].Value);
even though when I insert messagebox for
Convert.ToString(dataGridView2.SelectedRows[0].Cells[0].Value)
it shows the correct value when clicking on the row in datagridview