Hier eine kleine Funktion um ein beliebiges Objekt in einen gültigen Integer-Wert umzuwandeln:
static public int ToValidInt(object n) {
if(n==null) return 0;
if(n.GetType() == typeof(string)) {
try {
return int.Parse((string)n);
}
catch( Exception ) {
return 0;
}
}
if(n.GetType() == typeof(int)) {
return (int)n;
}
if(n.GetType() == typeof(decimal)) {
return decimal.ToInt32((decimal)n);
}
return 0;
}