private void PerformRowAdd() { try { NameValueCollection values = new NameValueCollection(); foreach (String text in HttpContext.Current.Request.Form.Keys) { values[text] = HttpContext.Current.Request.Form[text]; } GridRowAdd e = new GridRowAdd(); e.RowData = values; //String text2 = HttpContext.Current.Request.QueryString["parentRowID"]; //if (!String.IsNullOrEmpty(text2)) //{ // e.ParentRowKey = text2; //} Hashtable parentKeys = new Hashtable(); foreach (String key in HttpContext.Current.Request.QueryString.Keys) { if (key.NotEquals("jqGridID") && key.NotEquals("editMode") && key.NotEquals("SessionVarName")) { parentKeys.Add(key, HttpContext.Current.Request.QueryString[key]); } } e.ParentRowKeys = parentKeys; HandleInsert(e); } catch (Exception ex) { throw ex; } }
private void HandleInsert(GridRowAdd e) { try { IList retrievedData = (IList)HttpContext.Current.Session[SessionVarName]; Type actualType = retrievedData.GetType(); //Commented - Not checking whethere the object "actualType" is a List/CustomList type or not. It is assumed that //this object is always List/CustomList type. //Type genericType = typeof(CustomList<>); //while ((actualType != typeof(object)) && !(actualType.IsGenericType && (actualType.GetGenericTypeDefinition() == genericType))) //{ // actualType = actualType.BaseType; //} if (actualType == typeof(object)) { throw new System.ArgumentException("Object is not a CustomList<T> or List<T> instance.", "Obj"); } Type itemType = actualType.GetGenericArguments()[0]; Object newItem = Activator.CreateInstance(itemType); try { retrievedData.Add(newItem); } catch { throw new Exception("No record found!"); } try { String celValue = String.Empty; foreach (String key in e.RowData.AllKeys) { if (key.Equals("oper")) { continue; } PropertyInfo property = newItem.GetType().GetProperty(key); if (property.IsNull()) { continue; } if (e.ParentRowKeys.Contains(key)) { celValue = e.ParentRowKeys[key].ToString(); } else { celValue = e.RowData[key]; } String format = String.Empty; Object[] attribs = property.GetCustomAttributes(typeof(CustomAttributes.FormatString), false); if ((attribs.Length > 0) && ((CustomAttributes.FormatString)attribs[0]).Format.IsNotNullOrEmpty()) { format = ((CustomAttributes.FormatString)attribs[0]).Format; } Object value = GetActualValueFromGridRow(property.PropertyType.ToString(), celValue, format); property.SetValue(newItem, value, null); } } catch { throw new Exception("Error occured while assigning new value into object"); } } catch (Exception ex) { throw ex; } }
private void PerformRowAddDummy() { try { NameValueCollection values = new NameValueCollection(); foreach (String str in HttpContext.Current.Request.QueryString.Keys) { if (str.NotEquals("oper") && str.NotEquals("SessionVarName") && str.NotEquals("addDummyRecord") && str.NotEquals("jqGridID")) { values[str] = HttpContext.Current.Request.QueryString[str]; } } GridRowAdd e = new GridRowAdd { RowData = values, }; IList retrievedData = (IList)HttpContext.Current.Session[SessionVarName]; Type actualType = retrievedData.GetType(); if (actualType == typeof(Object)) { throw new System.ArgumentException("Object is not a CustomList<T> or List<T> instance.", "Obj"); } Type itemType = actualType.GetGenericArguments()[0]; Object newItem = Activator.CreateInstance(itemType); foreach (String key in e.RowData.AllKeys) { if (key.Equals("id")) { continue; } PropertyInfo property = newItem.GetType().GetProperty(key); if (property.IsNull()) { continue; } String format = String.Empty; Object[] attribs = property.GetCustomAttributes(typeof(CustomAttributes.FormatString), false); if ((attribs.Length > 0) && ((CustomAttributes.FormatString)attribs[0]).Format.IsNotNullOrEmpty()) { format = ((CustomAttributes.FormatString)attribs[0]).Format; } Object value = GetActualValueFromGridRow(property.PropertyType.ToString(), e.RowData[key], format); property.SetValue(newItem, value, null); } try { ((BaseItem)newItem).SetDetached(); retrievedData.Add(newItem); } catch { throw new Exception("No record found!"); } } catch (Exception ex) { throw ex; } }