Ejemplo n.º 1
0
        /// <summary>
        /// 绑定Grid中的ComboBox的DataSource
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="control"></param>
        /// <param name="list"></param>
        /// <param name="displayMemberPropertyName"></param>
        /// <param name="valueMemberPropertyName"></param>
        public static void BindComboBoxItems <T>(RepositoryItemComboBox control, object list, string displayMemberPropertyName, string valueMemberPropertyName)
        {
            if (list == null)
            {
                return;
            }
            ICollection collection = list as ICollection;

            if (collection == null)
            {
                return;
            }

            CustomDisplayDataSource items = new CustomDisplayDataSource();

            PropertyInfo propInfo = null;

            foreach (T entity in collection)
            {
                try
                {
                    propInfo = entity.GetType().GetProperty(displayMemberPropertyName);
                    object displayMember = propInfo.GetValue(entity, null);
                    propInfo = entity.GetType().GetProperty(valueMemberPropertyName);
                    object valueMember = propInfo.GetValue(entity, null);
                    if (displayMember != null && valueMember != null)
                    {
                        items.AddItem(valueMember, displayMember.ToString());
                    }
                }
                catch { }
            }

            BindComboBoxItems(control, items);
        }
Ejemplo n.º 2
0
 private static void BindComboBoxItems(RepositoryItemComboBox control, CustomDisplayDataSource items)
 {
     control.BeginUpdate();
     control.Items.Clear();
     control.Items.AddRange((System.Collections.ICollection)items);
     control.EditFormat.FormatType = control.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
     control.EditFormat.Format     = control.DisplayFormat.Format = items.FormatProvider;
     control.TextEditStyle         = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
     control.EndUpdate();
 }