Exemple #1
0
        private void SetBoundValue(IDataItem dataItem, string propertyName, object value)
        {
            object dataBoundItem = dataItem.DataBoundItem;

            if (dataBoundItem == null)
            {
                return;
            }
            Type type = dataBoundItem.GetType();

            if (this.propertyDescriptors == null)
            {
                this.propertyDescriptors = new Dictionary <Type, PropertyDescriptorCollection>();
            }
            if (!this.propertyDescriptors.ContainsKey(type))
            {
                this.propertyDescriptors.Add(type, TypeDescriptor.GetProperties(dataBoundItem));
            }
            PropertyDescriptor propertyDescriptor = this.propertyDescriptors[type].Find(propertyName, !this.Template.ListSource.UseCaseSensitiveFieldNames);

            if (propertyDescriptor == null)
            {
                this.propertyDescriptors[type] = TypeDescriptor.GetProperties(dataBoundItem);
                propertyDescriptor             = this.propertyDescriptors[type].Find(propertyName, !this.Template.ListSource.UseCaseSensitiveFieldNames);
            }
            if (propertyDescriptor == null)
            {
                return;
            }
            GridViewObjectRelationalDataProvider hierarchyDataProvider = this.Template.HierarchyDataProvider as GridViewObjectRelationalDataProvider;

            hierarchyDataProvider?.SuspendNotifications();
            if (value == null)
            {
                try
                {
                    propertyDescriptor.SetValue(dataBoundItem, value);
                }
                catch
                {
                    propertyDescriptor.SetValue(dataBoundItem, (object)DBNull.Value);
                }
            }
            else
            {
                Type underlyingType = Nullable.GetUnderlyingType(propertyDescriptor.PropertyType);
                if (propertyDescriptor.Converter != null && (object)underlyingType != null && underlyingType.IsGenericType)
                {
                    value = propertyDescriptor.Converter.ConvertFromInvariantString(value.ToString());
                }
                propertyDescriptor.SetValue(dataBoundItem, value);
            }
            hierarchyDataProvider?.ResumeNotifications();
        }