// customize the columns of a C1FlexGrid control // use dynamic type in order to avoid dependencies. static void CustomizeFlexGrid(dynamic flex, Type entType, EntityDataSource entDataSource) { // configure columns foreach (dynamic c in flex.Cols) { // if the column is a key, make it read-only var pi = entType.GetProperty(c.Name); if (pi != null) { var atts = pi.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false); if (atts.Length > 0) { var att = atts[0] as EdmScalarPropertyAttribute; if (att.EntityKeyProperty) { c.AllowEditing = false; } } // if the column holds entities, give it a data map var type = pi.PropertyType; if (!type.IsPrimitive)// typeof(EntityObject).IsAssignableFrom(type)) { c.DataMap = entDataSource.GetLookupDictionary(type); } } } }
void _btnRefresh_Click(object sender, EventArgs e) { if (EntityDataSource != null) { EntityDataSource.Refresh(); } }
void _btnCancel_Click(object sender, EventArgs e) { if (EntityDataSource != null) { EntityDataSource.CancelChanges(); } }
// save/cancel/refresh void _btnSave_Click(object sender, EventArgs e) { if (EntityDataSource != null) { EntityDataSource.SaveChanges(); } }
public CollectionPropertyDescriptor(EntityDataSource ds, PropertyDescriptor pd) : base(pd.Name, null) { _ds = ds; _pd = pd; var elementType = _pd.PropertyType.GetGenericArguments()[0]; _listType = typeof(EntityBindingList <>); _listType = _listType.MakeGenericType(elementType); }
ListDictionary _dctLookup; // lookup dictionary (used to show and edit related entities in grid cells) #endregion //------------------------------------------------------------------------- #region ** ctor /// <summary> /// Initializes a new instance of a <see cref="EntitySet"/>. /// </summary> /// <param name="ds"><see cref="EntityDataSource"/> that owns the entities.</param> /// <param name="pi"><see cref="PropertyInfo"/> used to retrieve the set from the context.</param> internal EntitySet(EntityDataSource ds, PropertyInfo pi) { var type = pi.PropertyType; Debug.Assert( type.IsGenericType && type.GetGenericTypeDefinition() == typeof(DbSet <>) && type.GetGenericArguments().Length == 1); _ds = ds; _pi = pi; _elementType = type.GetGenericArguments()[0]; }
// customize the columns of a DataGridView static void CustomizeDataGridView(DataGridView dgv, Type entType, EntityDataSource entDataSource) { // configure columns for (int colIndex = 0; colIndex < dgv.Columns.Count; colIndex++) { // get column var c = dgv.Columns[colIndex]; // if the column is a key, make it read-only var pi = entType.GetProperty(c.DataPropertyName); if (pi != null) { var atts = pi.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false); if (atts.Length > 0) { var att = atts[0] as EdmScalarPropertyAttribute; if (att.EntityKeyProperty) { c.ReadOnly = true; } } // if the column holds entities, give it a data map var type = pi.PropertyType; if (!type.IsPrimitive) //if (typeof(EntityObject).IsAssignableFrom(type)) { var map = entDataSource.GetLookupDictionary(type); if (map != null) { var col = new DataGridViewComboBoxColumn(); col.HeaderText = c.HeaderText; col.DataPropertyName = c.DataPropertyName; col.Width = c.Width; col.DefaultCellStyle = c.DefaultCellStyle; col.DataSource = map; col.ValueMember = "Key"; col.DisplayMember = "Value"; dgv.Columns.RemoveAt(colIndex); dgv.Columns.Insert(colIndex, col); } } } } }
//---------------------------------------------------------------------------- #region ** ctor /// <summary> /// Initializes a new instance of a <see cref="EntityBindingList"/>. /// </summary> /// <param name="dataSource"><see cref="EntityDataSource"/> that owns the entities on this list.</param> /// <param name="query"><see cref="IEnumerable"/> that provides the entities for this list.</param> /// <param name="name">A name for identifying this list in its parent (for hierarchical binding).</param> public EntityBindingList(EntityDataSource dataSource, IEnumerable query, string name) { _ds = dataSource; _set = query as DbSet <T>; _coll = query as EntityCollection <T>; _query = query; _name = name; // populate list if (dataSource != null && !dataSource.DesignMode) { // at run time, get the data Refresh(); } else { // at design time, add one element to help with binding try { AddNew(); } catch { } } }