You are casting using as
. This says to the compiler, 'I am not sure about this cast so if it is invalid return a null'. In some cases this is what you want, but in most cases you want an exception to be thrown on invalid casts. In the latter case you cast using (SomeType)myValue
, so that if the cast is not valid, you will get an InvalidCastException
.
I short, you are performing an invalid cast on the line:
drv = ltv_rules.Items.GetItemAt(i) as DataRowView;
I hope this helps.