protected override void CopyProperties(DataControlField newField)
        {
            base.CopyProperties(newField);
            AutoGeneratedField field = (AutoGeneratedField)newField;

            field.DataType = DataType;
        }
Пример #2
0
		public void AutoGeneratedField_AssignProperty ()
		{
			AutoGeneratedField field = new AutoGeneratedField ("test");
			Assert.AreEqual ("test", field.DataField, "DataField");
			field.DataType = typeof (bool);
			Assert.AreEqual (typeof (bool), field.DataType, "DataType");
		}
Пример #3
0
		public void AutoGeneratedField_DefaultProperty ()
		{
			AutoGeneratedField field = new AutoGeneratedField ("test");
			Assert.AreEqual (true, field.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
			Assert.AreEqual (string.Empty, field.DataFormatString, "DataFormatString");
			Assert.AreEqual (true, field.InsertVisible, "InsertVisible");
		}
Пример #4
0
		public void AutoGeneratedField_ExtractValuesFromCell()
		{
			AutoGeneratedField field = new AutoGeneratedField ("field");
			OrderedDictionary dictionary = new OrderedDictionary ();
			DataControlFieldCell cell = new DataControlFieldCell (null);
			cell.Text = "cell";
			field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
			Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount");
			Assert.AreEqual ("cell", dictionary[0].ToString (), "ExtractValuesFromCellValue");
		}
        /// <devdoc>
        /// Create a single autogenerated field.  This function can be overridden to create a different AutoGeneratedField.
        /// </devdoc>
        protected virtual AutoGeneratedField CreateAutoGeneratedFieldFromFieldProperties(AutoGeneratedFieldProperties fieldProperties) {
            AutoGeneratedField field = new AutoGeneratedField(fieldProperties.DataField);
            string name = fieldProperties.Name;
            ((IStateManager)field).TrackViewState();

            field.HeaderText = name;
            field.SortExpression = name;
            field.ReadOnly = fieldProperties.IsReadOnly;
            field.DataType = fieldProperties.Type;

            return field;
        }
Пример #6
0
        /// <devdoc>
        /// Create a single autogenerated field.  This function can be overridden to create a different AutoGeneratedField.
        /// </devdoc>
        protected virtual AutoGeneratedField CreateAutoGeneratedFieldFromFieldProperties(AutoGeneratedFieldProperties fieldProperties)
        {
            AutoGeneratedField field = new AutoGeneratedField(fieldProperties.DataField);
            string             name  = fieldProperties.Name;

            ((IStateManager)field).TrackViewState();

            field.HeaderText     = name;
            field.SortExpression = name;
            field.ReadOnly       = fieldProperties.IsReadOnly;
            field.DataType       = fieldProperties.Type;

            return(field);
        }
Пример #7
0
		public void AutoGeneratedField_ExtractValuesFromCellCheckbox ()
		{
			// Aditional implementation for bollean data type

			AutoGeneratedField field = new AutoGeneratedField ("field");
			field.DataType = typeof (bool);
			OrderedDictionary dictionary = new OrderedDictionary ();
			DataControlFieldCell cell = new DataControlFieldCell(null);
			cell.Controls.Add (new CheckBox ());
			field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
			Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount");
			Assert.AreEqual ("False", dictionary[0].ToString (), "ExtractValuesFromCellValue");
			CheckBox cb = new CheckBox ();
			cb.Checked = true;
			cell.Controls.Clear ();
			cell.Controls.Add (cb);
			field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
			Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount");
			Assert.AreEqual ("True", dictionary[0].ToString (), "ExtractValuesFromCellValue");
		}
Пример #8
0
		public void AutoGeneratedField_DefaultPropertyNotWorking ()
		{
			AutoGeneratedField field = new AutoGeneratedField ("test");
			Assert.AreEqual (typeof (String), field.DataType, "DataType");
		}
Пример #9
0
		public void AutoGeneratedField_InsertVisibleExeption ()
		{
			AutoGeneratedField field = new AutoGeneratedField ("test");
			field.InsertVisible = false;
		}
Пример #10
0
		public void AutoGeneratedField_DataFormatStringExeption ()
		{
			AutoGeneratedField field = new AutoGeneratedField ("test");
			field.DataFormatString = "test";
		}
Пример #11
0
		public void AutoGeneratedField_ConvertEmptyStringToNullExeption ()
		{
			AutoGeneratedField field = new AutoGeneratedField ("test");
			field.ConvertEmptyStringToNull = false;	

		}
Пример #12
0
        public override List <AutoGeneratedField> CreateAutoGeneratedFields(object dataItem, Control control)
        {
            if (!(control is DetailsView))
            {
                throw new ArgumentException(SR.GetString(SR.InvalidDefaultAutoFieldGenerator, GetType().FullName, typeof(DetailsView).FullName));
            }

            DetailsView detailsView = control as DetailsView;

            if (dataItem == null)
            {
                // note that we're not throwing an exception in this case, and the calling
                // code should be able to handle a null arraylist being returned
                return(null);
            }

            List <AutoGeneratedField>    generatedFields = new List <AutoGeneratedField>();
            PropertyDescriptorCollection propDescs       = null;
            bool throwException = true;
            Type dataItemType   = null;

            //The base class ensures that the AutoGeneratedFieldProperties collection is reset before this method is called.
            //However we are doing this again in here because we have another caller DetailsView.CreateAutoGeneratedRows which is
            //not being used anywhere today but kept for backward compatibility.
            if (AutoGeneratedFieldProperties.Count > 0)
            {
                AutoGeneratedFieldProperties.Clear();
            }

            if (dataItem != null)
            {
                dataItemType = dataItem.GetType();
            }

            if ((dataItem != null) && (dataItem is ICustomTypeDescriptor))
            {
                // Get the custom properties of the object
                propDescs = TypeDescriptor.GetProperties(dataItem);
            }
            else if (dataItemType != null)
            {
                // directly bindable types: strings, ints etc. get treated specially, since we
                // don't care about their properties, but rather we care about them directly
                if (ShouldGenerateField(dataItemType, detailsView))
                {
                    AutoGeneratedFieldProperties fieldProps = new AutoGeneratedFieldProperties();
                    ((IStateManager)fieldProps).TrackViewState();

                    fieldProps.Name      = "Item";
                    fieldProps.DataField = AutoGeneratedField.ThisExpression;
                    fieldProps.Type      = dataItemType;

                    AutoGeneratedField field = CreateAutoGeneratedFieldFromFieldProperties(fieldProps);
                    if (field != null)
                    {
                        generatedFields.Add(field);
                        AutoGeneratedFieldProperties.Add(fieldProps);
                    }
                }
                else
                {
                    // complex type... we get its properties
                    propDescs = TypeDescriptor.GetProperties(dataItemType);
                }
            }

            if ((propDescs != null) && (propDescs.Count != 0))
            {
                string[] dataKeyNames   = detailsView.DataKeyNames;
                int      keyNamesLength = dataKeyNames.Length;
                string[] dataKeyNamesCaseInsensitive = new string[keyNamesLength];
                for (int i = 0; i < keyNamesLength; i++)
                {
                    dataKeyNamesCaseInsensitive[i] = dataKeyNames[i].ToLowerInvariant();
                }
                foreach (PropertyDescriptor pd in propDescs)
                {
                    Type propertyType = pd.PropertyType;
                    if (ShouldGenerateField(propertyType, detailsView))
                    {
                        string name  = pd.Name;
                        bool   isKey = ((IList)dataKeyNamesCaseInsensitive).Contains(name.ToLowerInvariant());
                        AutoGeneratedFieldProperties fieldProps = new AutoGeneratedFieldProperties();
                        ((IStateManager)fieldProps).TrackViewState();

                        fieldProps.Name       = name;
                        fieldProps.IsReadOnly = isKey;
                        fieldProps.Type       = propertyType;
                        fieldProps.DataField  = name;

                        AutoGeneratedField field = CreateAutoGeneratedFieldFromFieldProperties(fieldProps);
                        if (field != null)
                        {
                            generatedFields.Add(field);
                            AutoGeneratedFieldProperties.Add(fieldProps);
                        }
                    }
                }
            }

            if ((generatedFields.Count == 0) && throwException)
            {
                // this handles the case where we got back something that either had no
                // properties, or all properties were not bindable.
                throw new InvalidOperationException(SR.GetString(SR.DetailsView_NoAutoGenFields, detailsView.ID));
            }

            return(generatedFields);
        }
        public override List <AutoGeneratedField> CreateAutoGeneratedFields(object dataObject, Control control)
        {
            if (!(control is GridView))
            {
                throw new ArgumentException(SR.GetString(SR.InvalidDefaultAutoFieldGenerator, GetType().FullName, typeof(GridView).FullName));
            }

            Debug.Assert(dataObject == null || dataObject is PagedDataSource);

            PagedDataSource dataSource = dataObject as PagedDataSource;
            GridView        gridView   = control as GridView;

            if (dataSource == null)
            {
                // note that we're not throwing an exception in this case, and the calling
                // code should be able to handle a null arraylist being returned
                return(null);
            }

            List <AutoGeneratedField>    generatedFields = new List <AutoGeneratedField>();
            PropertyDescriptorCollection propDescs       = null;
            bool throwException = true;

            // try ITypedList first
            // A PagedDataSource implements this, but returns null, if the underlying data source
            // does not implement it.
            propDescs = ((ITypedList)dataSource).GetItemProperties(new PropertyDescriptor[0]);

            if (propDescs == null)
            {
                Type   sampleItemType = null;
                object sampleItem     = null;

                IEnumerable realDataSource = dataSource.DataSource;
                Debug.Assert(realDataSource != null, "Must have a real data source when calling CreateAutoGeneratedColumns");

                Type dataSourceType = realDataSource.GetType();

                // try for a typed Row property, which should be present on strongly typed collections
                PropertyInfo itemProp = dataSourceType.GetProperty("Item", BindingFlags.Public | BindingFlags.Instance, null, null, new Type[] { typeof(int) }, null);
                if (itemProp != null)
                {
                    sampleItemType = itemProp.PropertyType;
                }

                if ((sampleItemType == null) || (sampleItemType == typeof(object)))
                {
                    // last resort... try to get ahold of the first item by beginning the
                    // enumeration

                    IEnumerator e = dataSource.GetEnumerator();

                    if (e.MoveNext())
                    {
                        sampleItem = e.Current;
                    }
                    else
                    {
                        // we don't want to throw an exception if we're bound to an IEnumerable
                        // data source with no records... we'll simply bail and not show any data
                        throwException = false;
                    }
                    if (sampleItem != null)
                    {
                        sampleItemType = sampleItem.GetType();
                    }

                    // We must store the enumerator regardless of whether we got back an item from it
                    // because we cannot start the enumeration again, in the case of a DataReader.
                    // Code in CreateChildControls must deal appropriately for the case where
                    // there is a stored enumerator, but a null object as the first item.
                    gridView.StoreEnumerator(e, sampleItem);
                }

                if ((sampleItem != null) && (sampleItem is ICustomTypeDescriptor))
                {
                    // Get the custom properties of the object
                    propDescs = TypeDescriptor.GetProperties(sampleItem);
                }
                else if (sampleItemType != null)
                {
                    // directly bindable types: strings, ints etc. get treated specially, since we
                    // don't care about their properties, but rather we care about them directly
                    if (ShouldGenerateField(sampleItemType, gridView))
                    {
                        AutoGeneratedFieldProperties fieldProps = new AutoGeneratedFieldProperties();
                        ((IStateManager)fieldProps).TrackViewState();

                        fieldProps.Type      = sampleItemType;
                        fieldProps.Name      = "Item";
                        fieldProps.DataField = AutoGeneratedField.ThisExpression;

                        AutoGeneratedField field = CreateAutoGeneratedFieldFromFieldProperties(fieldProps);
                        if (field != null)
                        {
                            generatedFields.Add(field);

                            AutoGeneratedFieldProperties.Add(fieldProps);
                        }
                    }
                    else
                    {
                        // complex type... we get its properties
                        propDescs = TypeDescriptor.GetProperties(sampleItemType);
                    }
                }
            }
            else
            {
                if (propDescs.Count == 0)
                {
                    // we don't want to throw an exception if we're bound to an ITypedList
                    // data source with no records... we'll simply bail and not show any data
                    throwException = false;
                }
            }

            if ((propDescs != null) && (propDescs.Count != 0))
            {
                string[] dataKeyNames   = gridView.DataKeyNames;
                int      keyNamesLength = dataKeyNames.Length;
                string[] dataKeyNamesCaseInsensitive = new string[keyNamesLength];
                for (int i = 0; i < keyNamesLength; i++)
                {
                    dataKeyNamesCaseInsensitive[i] = dataKeyNames[i].ToLowerInvariant();
                }
                foreach (PropertyDescriptor pd in propDescs)
                {
                    Type propertyType = pd.PropertyType;
                    if (ShouldGenerateField(propertyType, gridView))
                    {
                        string name  = pd.Name;
                        bool   isKey = ((IList)dataKeyNamesCaseInsensitive).Contains(name.ToLowerInvariant());
                        AutoGeneratedFieldProperties fieldProps = new AutoGeneratedFieldProperties();
                        ((IStateManager)fieldProps).TrackViewState();
                        fieldProps.Name       = name;
                        fieldProps.IsReadOnly = isKey;
                        fieldProps.Type       = propertyType;
                        fieldProps.DataField  = name;

                        AutoGeneratedField field = CreateAutoGeneratedFieldFromFieldProperties(fieldProps);
                        if (field != null)
                        {
                            generatedFields.Add(field);
                            AutoGeneratedFieldProperties.Add(fieldProps);
                        }
                    }
                }
            }

            if ((generatedFields.Count == 0) && throwException)
            {
                // this handles the case where we got back something that either had no
                // properties, or all properties were not bindable.
                throw new InvalidOperationException(SR.GetString(SR.GridView_NoAutoGenFields, gridView.ID));
            }

            return(generatedFields);
        }