/// <summary> /// Add or Update one record /// </summary> /// <param name="rowIndex">current row index</param> /// <param name="startCol">start column index</param> /// <param name="endCol">end column index</param> private void AddOrUpdateOneFromUi(int rowIndex, int startCol, int endCol) { IRecordModel record; if (rowIndex < _Records.Count) // update record { record = _Records.ElementAt(rowIndex); } else // insert record { record = Activator.CreateInstance(_RecordModelType) as IRecordModel; record.RowIndex = rowIndex; } for (int colIndex = startCol; colIndex <= endCol; colIndex++) { PropertyInfo propertyInfo = (PropertyInfo)_Worksheet.ColumnHeaders[colIndex].Tag; object cellData = _Worksheet.GetCellData(rowIndex, colIndex); object value = null; if (cellData != null) { if (propertyInfo.PropertyType.BaseType == typeof(Enum)) { try { value = Convert.ChangeType(Enum.Parse(propertyInfo.PropertyType, cellData.ToString()), propertyInfo.PropertyType); } catch (Exception ex) { #if DEBUG Console.WriteLine(ex.Message); break; #endif } } else { try { value = Convert.ChangeType(cellData, propertyInfo.PropertyType); } catch (Exception ex) { #if DEBUG Console.WriteLine(ex.Message); break; #endif } } } if (OnBeforeChangeRecord != null) { bool?isCancel = OnBeforeChangeRecord(record, propertyInfo, value); if (isCancel.HasValue && isCancel.Value) // if has value and cancel is true, then undo the change { _Worksheet.SetCellData(rowIndex, colIndex, propertyInfo.GetValue(record)); //if (rowIndex < _Records.Count) //{ // _Worksheet.SetCellData(rowIndex, colIndex, propertyInfo.GetValue(record)); //} //else //{ //} continue; } } propertyInfo.SetValue(record, value); } if (rowIndex >= _Records.Count) { _Records.Add(record); } }