We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Putting a custom user control in the grouped grid.

Hi - I am trying to get a custom user control to drop down from a cell in a grouped grid. To make matters more complicated, the control needs to be instantiated with values from the row in which it is embedded (or, if we can''t instatiate it then, at least call a method on drop down passing in those values). I''ve tried copying the sample in the samples browser, but am having a little trouble. I haven''t even gotten upto the bit where values are passed in... i''m still just trying to get any control to display... Firstly - I''ve copied and changed the inherited GridStaticCellModel class from the sample. Note that ucDamo is a very simple user control. It has a button and a couple of text getters/setters. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.Serialization; using Syncfusion.Drawing; using Syncfusion.Windows.Forms; using Syncfusion.Windows.Forms.Grid; namespace Order_Promising_Workbench { // The model class is mainly derived to make // CreateRenderer return our derived renderer class. // Here we add an internal member, ddUser, to hold our UserControl // that we want to show in our popup. We set this member // in the constructor call. public class DropDownUserCellModel : GridStaticCellModel { internal ucDamo ddUser; protected DropDownUserCellModel(SerializationInfo info, StreamingContext context) : base(info, context) { ButtonBarSize = new Size(14, 14); } public DropDownUserCellModel(GridModel grid, ucDamo ddUser) : base(grid) { this.ddUser = ddUser; ButtonBarSize = new Size(14, 14); } //note that this method create our new derived renderer public override GridCellRendererBase CreateRenderer(GridControlBase control) { return new DropDownUserCellRenderer(control, this); } } //The renderer class. The constructor caches a reference to the UserControl, //adds the button, disables the text box from being typed into, and //subscribes to Save and Cancel events on teh User Control. public class DropDownUserCellRenderer: GridStaticCellRenderer { internal ucDamo ddUser; public DropDownUserCellRenderer(GridControlBase grid, GridCellModelBase cellModel) : base(grid, cellModel) { this.ddUser = ((DropDownUserCellModel)this.Model).ddUser; this.ddUser.Visible = false; DropDownPart = new GridDropDownCellImp(this); DropDownButton = new GridCellComboBoxButton(this); //hook the usercontrol save and cancel events... //this.ddUser.UserControlSave += new EventHandler(user_Save); //this.ddUser.UserControlCancel += new EventHandler(user_Cancel); this.ddUser.button1.Click+=new EventHandler(button1_Click); } public new GridDropDownContainer DropDownContainer { get{return (GridDropDownContainer)base.DropDownContainer;} } //here we add the user control to the popup window. protected override void InitializeDropDownContainer() { base.InitializeDropDownContainer(); if (this.DropDownContainer != null) { this.DropDownContainer.Controls.Add(this.ddUser); } } //used to setup the dropdown before it is shown. The RaiseCurrentCellShowingDropDown //code is commeted out as it requires teh 2.0 code base. This commented code allow //for an event that might cancel teh drop or set its size. public override void DropDownContainerShowingDropDown(object sender, CancelEventArgs e) { GridCurrentCell cc = this.Grid.CurrentCell; //// Size size = ddUser.Size; //// GridCurrentCellShowingDropDownEventArgs ce = new GridCurrentCellShowingDropDownEventArgs(size); //// Grid.RaiseCurrentCellShowingDropDown(ce); //// if (ce.Cancel) //// { //// e.Cancel = true; //// return; //// } //// ddUser.Size = ce.Size; /// this.DropDownContainer.Size = ddUser.Size; this.ddUser.SetValuesFromString(this.ControlText); ddUser.Visible = true; } //used to change teh ControlValue if dropdown was closed with Done. public override void DropDownContainerCloseDropDown(object sender, PopupClosedEventArgs e) { if (e.PopupCloseType == PopupCloseType.Done) { if (this.NotifyCurrentCellChanging()) { ControlValue = this.ddUser.GetValuesToString; this.NotifyCurrentCellChanged(); } } Grid.InvalidateRange(GridRangeInfo.Cell(RowIndex, ColIndex), GridRangeOptions.MergeCoveredCells); // Merge all cells base.DropDownContainerCloseDropDown(sender, e); } //handler for the user control Save event private void user_Save(object sender, EventArgs e) { // closethe dropdown with a Done setting this.Grid.CurrentCell.CloseDropDown(Syncfusion.Windows.Forms.PopupCloseType.Done); } //handler for the user control Cancel event private void user_Cancel(object sender, EventArgs e) { // close the dropdown with a Canceled setting this.Grid.CurrentCell.CloseDropDown(Syncfusion.Windows.Forms.PopupCloseType.Canceled); } //used to move the grid value into the cell control protected override void OnInitialize(int rowIndex, int colIndex) { base.OnInitialize (rowIndex, colIndex); ddUser.SetValuesFromString(this.Grid.Model[rowIndex, colIndex].Text); } //used to save the cell control value back to the grid protected override bool OnSaveChanges() { string s = this.ddUser.GetValuesToString; this.Grid.Model[this.RowIndex, this.ColIndex].Text = s; return true; } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Button Clicked"); } } } Secondly - on form initialisation, I ''register my custom cell type'' public void BindCustomDropDown() { this.grItems.TableModel.CellModels.Add("DropDownControl", new DropDownUserCellModel(this.grItems.TableModel.Model, new ucDamo())); } Thirdly (and this bit I''ve made up) - I handle the prepareviewstyle info event and try to apply my new type. I''m aware that if this worked, it would only put the control at 3,3... however in production I wish to have the dropdown on every row. if (e.Inner.RowIndex==3 && e.Inner.ColIndex==3) { this.grItems.TableModel[e.Inner.RowIndex, e.Inner.ColIndex].CellType = "DropDownControl"; } Thanks very much for your help. I appreciate that this question is a bit more complicated that my normal simple ones. Thanks in Advance, Damien Sawyer

1 Reply

AD Administrator Syncfusion Team August 23, 2005 07:03 AM UTC

Thanks - I found a solution though... I needed to set the GridColumnDescriptor... if (pi2.Name=="DropDownGrid") { gcd.Appearance.AnyRecordFieldCell.CellType = "DropDownControl"; }

Loader.
Live Chat Icon For mobile
Up arrow icon