Example T415077
Visible to All Users

WinForms Property Grid - Use a custom cell editor

This example demonstrates how to implement a UI Type Editor (FilteredFileNameEditor) and use it within the PropertyGridControl.

  1. Create a FilteredFileNameEditor class and implement the UITypeEditor interface:
    C#
    internal class FilteredFileNameEditor : UITypeEditor { private OpenFileDialog ofd = new OpenFileDialog(); public override UITypeEditorEditStyle GetEditStyle( ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value) { ofd.FileName = value.ToString(); ofd.Filter = "Text File|*.txt|All Files|*.*"; if (ofd.ShowDialog() == DialogResult.OK) { return ofd.FileName; } return base.EditValue(context, provider, value); } }
  2. Apply the System.ComponentModel.Editor attribute as follows:
    C#
    public class File { [System.ComponentModel.Editor(typeof(UIEditors.FilteredFileNameEditor), typeof(System.Drawing.Design.UITypeEditor))] public string Path { get; set; } public string Path2 { get; set; } }
  3. Assign a ButtonEdit to a cell as shown in the Assigning Editors to Editor Rows topic.
    C#
    private void Form1_Shown(object sender, EventArgs e) { RepositoryItemButtonEdit edit = new RepositoryItemButtonEdit(); edit.ButtonClick += edit_ButtonClick; (this.propertyGridControl1.Rows[0] as CategoryRow).ChildRows["rowPath2"].Properties.RowEdit = edit; }
  4. Handle the Button Editor's ButtonClick event.
    C#
    void edit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { this.openFileDialog1.ShowDialog(); }

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

T415077/Data/File.cs(vb)
C#
namespace T415077.Data { public class File { [System.ComponentModel.Editor(typeof(UIEditors.FilteredFileNameEditor), typeof(System.Drawing.Design.UITypeEditor))] public string Path { get; set; } public string Path2 { get; set; } } }
T415077/Form1.cs(vb)
C#
using DevExpress.XtraEditors; using DevExpress.XtraEditors.Repository; using DevExpress.XtraVerticalGrid.Rows; using System; namespace T415077 { public partial class Form1 : XtraForm { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Data.File file = new Data.File() { Path = "C:\\Home", Path2 = "C:\\Public" }; this.propertyGridControl1.SelectedObject = file; } void edit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { this.openFileDialog1.ShowDialog(); } private void Form1_Shown(object sender, EventArgs e) { RepositoryItemButtonEdit edit = new RepositoryItemButtonEdit(); edit.ButtonClick += edit_ButtonClick; (this.propertyGridControl1.Rows[0] as CategoryRow).ChildRows["rowPath2"].Properties.RowEdit = edit; } } }
T415077/UIEditors/FilteredFileNameEditor.cs(vb)
C#
using System; using System.ComponentModel; using System.Drawing.Design; using System.Windows.Forms; namespace T415077.UIEditors { internal class FilteredFileNameEditor : UITypeEditor { private OpenFileDialog ofd = new OpenFileDialog(); public override UITypeEditorEditStyle GetEditStyle( ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value) { ofd.FileName = value.ToString(); ofd.Filter = "Text File|*.txt|All Files|*.*"; if (ofd.ShowDialog() == DialogResult.OK) { return ofd.FileName; } return base.EditValue(context, provider, value); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.