private void OnAutoFormat(object sender, EventArgs e)
        {
            object   component = base.Component;
            DataGrid dgrid     = component as DataGrid;
            DataGridAutoFormatDialog dataGridAutoFormatDialog = System.Windows.Forms.DpiHelper.CreateInstanceInSystemAwareContext(() => new DataGridAutoFormatDialog(dgrid));

            if (dataGridAutoFormatDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            DataRow             selectedData        = dataGridAutoFormatDialog.SelectedData;
            IDesignerHost       designerHost        = (IDesignerHost)GetService(typeof(IDesignerHost));
            DesignerTransaction designerTransaction = designerHost.CreateTransaction(SR.GetString("DataGridAutoFormatUndoTitle", base.Component.Site.Name));

            try
            {
                if (selectedData != null)
                {
                    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(DataGrid));
                    foreach (DataColumn column in selectedData.Table.Columns)
                    {
                        object             obj = selectedData[column];
                        PropertyDescriptor propertyDescriptor = properties[column.ColumnName];
                        if (propertyDescriptor == null)
                        {
                            continue;
                        }
                        if (Convert.IsDBNull(obj) || obj.ToString().Length == 0)
                        {
                            propertyDescriptor.ResetValue(dgrid);
                            continue;
                        }
                        try
                        {
                            TypeConverter converter = propertyDescriptor.Converter;
                            object        value     = converter.ConvertFromString(obj.ToString());
                            propertyDescriptor.SetValue(dgrid, value);
                        }
                        catch
                        {
                        }
                    }
                }
            }
            finally
            {
                designerTransaction.Commit();
            }
            dgrid.Invalidate();
        }
        private void OnAutoFormat(object sender, EventArgs e)
        {
            DataGrid component = base.Component as DataGrid;
            DataGridAutoFormatDialog dialog = new DataGridAutoFormatDialog(component);

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                DataRow             selectedData = dialog.SelectedData;
                DesignerTransaction transaction  = ((IDesignerHost)this.GetService(typeof(IDesignerHost))).CreateTransaction(System.Design.SR.GetString("DataGridAutoFormatUndoTitle", new object[] { base.Component.Site.Name }));
                try
                {
                    if (selectedData != null)
                    {
                        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(DataGrid));
                        foreach (DataColumn column in selectedData.Table.Columns)
                        {
                            object             obj3       = selectedData[column];
                            PropertyDescriptor descriptor = properties[column.ColumnName];
                            if (descriptor != null)
                            {
                                if (Convert.IsDBNull(obj3) || (obj3.ToString().Length == 0))
                                {
                                    descriptor.ResetValue(component);
                                }
                                else
                                {
                                    try
                                    {
                                        object obj4 = descriptor.Converter.ConvertFromString(obj3.ToString());
                                        descriptor.SetValue(component, obj4);
                                    }
                                    catch
                                    {
                                    }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    transaction.Commit();
                }
                component.Invalidate();
            }
        }
 private void OnAutoFormat(object sender, EventArgs e)
 {
     DataGrid component = base.Component as DataGrid;
     DataGridAutoFormatDialog dialog = new DataGridAutoFormatDialog(component);
     if (dialog.ShowDialog() == DialogResult.OK)
     {
         DataRow selectedData = dialog.SelectedData;
         DesignerTransaction transaction = ((IDesignerHost) this.GetService(typeof(IDesignerHost))).CreateTransaction(System.Design.SR.GetString("DataGridAutoFormatUndoTitle", new object[] { base.Component.Site.Name }));
         try
         {
             if (selectedData != null)
             {
                 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(DataGrid));
                 foreach (DataColumn column in selectedData.Table.Columns)
                 {
                     object obj3 = selectedData[column];
                     PropertyDescriptor descriptor = properties[column.ColumnName];
                     if (descriptor != null)
                     {
                         if (Convert.IsDBNull(obj3) || (obj3.ToString().Length == 0))
                         {
                             descriptor.ResetValue(component);
                         }
                         else
                         {
                             try
                             {
                                 object obj4 = descriptor.Converter.ConvertFromString(obj3.ToString());
                                 descriptor.SetValue(component, obj4);
                             }
                             catch
                             {
                             }
                         }
                     }
                 }
             }
         }
         finally
         {
             transaction.Commit();
         }
         component.Invalidate();
     }
 }
예제 #4
0
        /// <include file='doc\DataGridDesigner.uex' path='docs/doc[@for="DataGridDesigner.OnAutoFormat"]/*' />
        /// <devdoc>
        ///    <para>Raises the AutoFormat event.</para>
        /// </devdoc>
        public void OnAutoFormat(object sender, EventArgs e)
        {
            object o = Component;

            Debug.Assert(o is DataGrid, "DataGrid type expected.");
            DataGrid dgrid = (DataGrid)o;
            DataGridAutoFormatDialog dialog = new DataGridAutoFormatDialog(dgrid);

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                DataRow selectedData = dialog.SelectedData;
                if (selectedData != null)
                {
                    PropertyDescriptorCollection gridProperties = TypeDescriptor.GetProperties(typeof(DataGrid));
                    foreach (DataColumn c in selectedData.Table.Columns)
                    {
                        object             value = selectedData[c];
                        PropertyDescriptor prop  = gridProperties[c.ColumnName];
                        if (prop != null)
                        {
                            if (Convert.IsDBNull(value) || value.ToString().Length == 0)
                            {
                                prop.ResetValue(dgrid);
                            }
                            else
                            {
                                try {
                                    TypeConverter converter      = prop.Converter;
                                    object        convertedValue = converter.ConvertFromString(value.ToString());
                                    prop.SetValue(dgrid, convertedValue);
                                }
                                catch (Exception) {
                                    // Ignore errors... the only one we really care about is Font names.
                                    // The TypeConverter will throw if the font isn't on the user's machine
                                }
                            }
                        }
                    }
                }
                // now invalidate the grid
                dgrid.Invalidate();
            }
        }