|
1 | | -# How to set folder browser cell in winforms gridcontrol |
2 | | -This example demonstrates how to set folder browser cell in winforms gridcontrol |
| 1 | +# How to Set Folder Browser Cell Type in WinForms GridControl? |
| 2 | + |
| 3 | +This example demonstrates how to set folder browser cell in [WinForms GridControl](https://www.syncfusion.com/winforms-ui-controls/grid-control). |
| 4 | + |
| 5 | +To use the `FolderBrowser` cell type, create custom `FolderBrowserCellModel` and `FolderBrowserCellRenderer` derived from [GridTextBoxCellModel](https://help.syncfusion.com/cr/windowsforms/Syncfusion.Windows.Forms.Grid.GridTextBoxCellModel.html) and [GridTextBoxCellRenderer](https://help.syncfusion.com/cr/windowsforms/Syncfusion.Windows.Forms.Grid.GridTextBoxCellRenderer.html). The Folder Browser dialog box will be displayed in the [OnButtonClicked](https://help.syncfusion.com/cr/windowsforms/Syncfusion.Windows.Forms.Grid.GridCellRendererBase.html#Syncfusion_Windows_Forms_Grid_GridCellRendererBase_OnButtonClicked_System_Int32_System_Int32_System_Int32_) method. |
| 6 | + |
| 7 | +### Creating CustomCellModel |
| 8 | + |
| 9 | +``` c# |
| 10 | +//Deriving GridTextBoxCellModel. |
| 11 | +public class FolderBrowserCellModel : GridTextBoxCellModel |
| 12 | +{ |
| 13 | + protected FolderBrowserCellModel(SerializationInfo info, StreamingContext context) |
| 14 | + : base(info, context) |
| 15 | + { |
| 16 | + //Set the button bar size. |
| 17 | + base.ButtonBarSize = new Size(20, 20); |
| 18 | + } |
| 19 | + //Constructor for the Derived Class |
| 20 | + public FolderBrowserCellModel(GridModel grid) |
| 21 | + : base(grid) |
| 22 | + { |
| 23 | + |
| 24 | + } |
| 25 | + //Override the CreateRenderer() in the Base class. |
| 26 | + public override GridCellRendererBase CreateRenderer(GridControlBase control) |
| 27 | + { |
| 28 | + //Return the Custom Renderer Object |
| 29 | + return new FolderBrowserCellRenderer(control, this); |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### Creating CustomCellRenderer |
| 35 | + |
| 36 | +``` c# |
| 37 | +//Deriving the GridTextBoxCellRenderer. |
| 38 | +public class FolderBrowserCellRenderer : GridTextBoxCellRenderer |
| 39 | +{ |
| 40 | + //FolderBrowser object declaration. |
| 41 | + private System.Windows.Forms.OpenFileDialog folderBrowser1; |
| 42 | + public FolderBrowserCellRenderer(GridControlBase grid, GridTextBoxCellModel cellModel) |
| 43 | + : base(grid, cellModel) |
| 44 | + { |
| 45 | + AddButton(new BrowseButton(this)); |
| 46 | + //Initialize the folderBrowser1 object. |
| 47 | + this.folderBrowser1 = new System.Windows.Forms.OpenFileDialog(); |
| 48 | + } |
| 49 | + #region [overrides] |
| 50 | + protected override void OnButtonClicked(int rowIndex, int colIndex, int button) |
| 51 | + { |
| 52 | + base.OnButtonClicked(rowIndex, colIndex, button); |
| 53 | + if(folderBrowser1.ShowDialog()== DialogResult.OK) |
| 54 | + { |
| 55 | + string filePath = folderBrowser1.FileName; |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### Adding CellModel |
| 62 | + |
| 63 | +``` c# |
| 64 | +// Add the custom cell type to the CellModels of the GridControl. |
| 65 | +this.gridControl1.CellModels.Add("FolderBrowser", new FolderBrowserCellModel(gridControl1.Model)); |
| 66 | +// Set the cell type to "FolderBrowser" |
| 67 | +this.gridControl1[2, 3].Text = "Browse here"; |
| 68 | +this.gridControl1[2, 3].CellType = "FolderBrowser"; |
| 69 | +``` |
0 commit comments