winforms - index was out of range. must be nonnegative and less than the size of the collection. in c# -


in application has 2 datagridviews named datagridview1 , datagridview2 , have selectionchanged event in datagridview1 captures data selected in content of datagridview2 changed based on product name of selected row in datagridview1 is. don't know why have index out of range because have checked have @ least 1 row , 2 columns in datagridview1. using dataview rowfilter function.

here code:

private void datagridview1_selectionchanged(object sender, eventargs e) {     view_1.rowfilter = "product_name = '" + datagridview1.selectedrows[0].cells[1].value.tostring() +"'"; } 

the selectionchanged event should fire when unselect rows well, selectedrows should empty , therefore cannot index @ all.

you should first check if it's length greater zero, , if is, continue function.

also, since see handle single selected row, suggest make sure don't allow multi-row-selection on datagridview control (unless needed else, such delete or whatever).


Comments