Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / WinForms / System / WinForms / Design / DataGridDesigner.cs / 1 / DataGridDesigner.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Windows.Forms.Design {
using System.Design;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel.Design;
using System.Drawing;
using Microsoft.Win32;
using System.Windows.Forms.ComponentModel;
///
///
/// Provides a base designer for data grids.
///
internal class DataGridDesigner : System.Windows.Forms.Design.ControlDesigner {
///
///
/// Gets the design-time verbs suppoted by the component associated with the
/// designer.
///
protected DesignerVerbCollection designerVerbs;
private IComponentChangeService changeNotificationService = null;
///
///
/// Initializes a new instance of the class.
///
private DataGridDesigner() {
designerVerbs = new DesignerVerbCollection();
designerVerbs.Add(new DesignerVerb(SR.GetString(SR.DataGridAutoFormatString), new EventHandler(this.OnAutoFormat)));
AutoResizeHandles = true;
}
public override void Initialize(IComponent component) {
base.Initialize(component);
IDesignerHost dh = (IDesignerHost) this.GetService(typeof(IDesignerHost));
if (dh != null) {
changeNotificationService = (IComponentChangeService) dh.GetService(typeof(IComponentChangeService));
if (changeNotificationService != null)
changeNotificationService.ComponentRemoved += new ComponentEventHandler(DataSource_ComponentRemoved);
}
}
private void DataSource_ComponentRemoved(object sender, ComponentEventArgs e) {
DataGrid d = (DataGrid) this.Component;
if (e.Component == d.DataSource)
d.DataSource = null;
}
protected override void Dispose(bool disposing) {
if (disposing) {
if (changeNotificationService != null)
changeNotificationService.ComponentRemoved -= new ComponentEventHandler(DataSource_ComponentRemoved);
}
base.Dispose(disposing);
}
///
///
/// Gets the design-time verbs supported by the component associated with the
/// designer.
///
public override DesignerVerbCollection Verbs {
get {
return designerVerbs;
}
}
///
///
/// Raises the AutoFormat event.
///
[
SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes") // See comment inside the method about ignoring errors.
]
private void OnAutoFormat(object sender, EventArgs e) {
object o = Component;
DataGrid dgrid = o as DataGrid;
Debug.Assert(dgrid != null, "DataGrid expected.");
DataGridAutoFormatDialog dialog = new DataGridAutoFormatDialog(dgrid);
if (dialog.ShowDialog() == DialogResult.OK) {
DataRow selectedData = dialog.SelectedData;
IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
DesignerTransaction trans = host.CreateTransaction(SR.GetString(SR.DataGridAutoFormatUndoTitle, Component.Site.Name));
try {
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 {
// 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
}
}
}
}
}
}
finally {
trans.Commit();
}
// now invalidate the grid
dgrid.Invalidate();
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DataSourceControlBuilder.cs
- ListViewGroupConverter.cs
- Win32Exception.cs
- ACE.cs
- DataGridViewTextBoxColumn.cs
- NavigationWindowAutomationPeer.cs
- XComponentModel.cs
- Soap.cs
- Synchronization.cs
- SelectingProviderEventArgs.cs
- EntityDataSourceView.cs
- DataDesignUtil.cs
- ReferencedCategoriesDocument.cs
- SliderAutomationPeer.cs
- CookielessHelper.cs
- ChannelSinkStacks.cs
- TextLineResult.cs
- ZoneButton.cs
- Point3DCollectionConverter.cs
- DataColumnSelectionConverter.cs
- StringStorage.cs
- WebPartEditorApplyVerb.cs
- ConditionalAttribute.cs
- _NegoStream.cs
- TypeNameConverter.cs
- DataBindingHandlerAttribute.cs
- NotifyInputEventArgs.cs
- GridEntry.cs
- GraphicsPathIterator.cs
- GenericUriParser.cs
- UriScheme.cs
- Scripts.cs
- RSAPKCS1SignatureDeformatter.cs
- StateElementCollection.cs
- ToolStripDropDownButton.cs
- BamlRecords.cs
- DataFormats.cs
- BindingManagerDataErrorEventArgs.cs
- DefaultObjectMappingItemCollection.cs
- QueueProcessor.cs
- WorkflowRuntimeServiceElement.cs
- SessionStateSection.cs
- Matrix3DStack.cs
- RequestCachePolicy.cs
- StrokeDescriptor.cs
- Tokenizer.cs
- CultureInfoConverter.cs
- Span.cs
- SystemFonts.cs
- LocalBuilder.cs
- CollectionDataContract.cs
- XmlUtf8RawTextWriter.cs
- WebPartCloseVerb.cs
- PropertyValueChangedEvent.cs
- CustomError.cs
- FileRecordSequenceCompletedAsyncResult.cs
- CodeTypeParameter.cs
- UrlAuthFailedErrorFormatter.cs
- SingleSelectRootGridEntry.cs
- FixedSOMSemanticBox.cs
- BrushMappingModeValidation.cs
- BindStream.cs
- DrawingImage.cs
- XamlToRtfParser.cs
- SevenBitStream.cs
- CollectionEditor.cs
- DiagnosticSection.cs
- TrayIconDesigner.cs
- WindowsUpDown.cs
- ComponentDispatcherThread.cs
- MailHeaderInfo.cs
- ValidationErrorCollection.cs
- PageAsyncTaskManager.cs
- HyperLink.cs
- SourceChangedEventArgs.cs
- XmlIterators.cs
- EntryIndex.cs
- ValueUtilsSmi.cs
- HMACSHA384.cs
- AssemblyBuilderData.cs
- AttributeData.cs
- BamlRecordReader.cs
- SafeMILHandle.cs
- SetterBase.cs
- BinarySerializer.cs
- TaskResultSetter.cs
- EventDescriptor.cs
- StringDictionaryEditor.cs
- ScrollEvent.cs
- PerformanceCounterPermission.cs
- SoapHeader.cs
- MouseGestureConverter.cs
- SemanticTag.cs
- Membership.cs
- TypeBinaryExpression.cs
- XmlILAnnotation.cs
- FieldMetadata.cs
- HtmlEncodedRawTextWriter.cs
- PropertyCondition.cs
- ReflectionPermission.cs