How to use a button as an editor?
Hi all,
I am a newbie to Dapfor grid, working on the trial version to find out if it satisfies my requirements. Getting stuck in 2 simple areas...hope you all can help
1. below is my sample custom editor, try to add a button into a cell, unfortunately it the button is not shown in the grid row
class ButtonEditor: UITypeEditorEx
{
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
//Get the editor service
IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
if (service != null)
{
//Create a control
using (Button someControl = new Button())
{
//Add a reaction on user click
someControl.Click += delegate
{
//User has clicked on the button inside of the dropdown box. Close the control.
service.CloseDropDown();
//Set a desired value
//value = _new_value_;
};
//Start editing with specified control.
//This call opens a modal dropdown box
service.DropDownControl(someControl);
}
}
return value;
}
public override StopEditReason EditCell(IGridEditorService service, Cell cell, StartEditReason reason)
{
//Create a control to be displayed in the cell
using (Button myButton = new Button())
{
myButton.Image = global::RaptorClient.Properties.Resources.add;
//Add reaction on user input
//myBox.SelectedIndexChanged += delegate
//{
// //Ask the service to close the control and leave the Application.DoEvents() loop
// service.CloseCellControl(StopEditReason.UserStop);
// //Apply the value
// cell.Value = myBox.Text;
//};
myButton.Click += delegate
{
service.CloseCellControl(StopEditReason.UserStop);
};
//Begin cell editing. This is a blocking method with internal Application.DoEvents() loop.
StopEditReason stotReason = service.CellEditControl(myButton, cell.VisibleBounds, reason);
//return from the editing procedure.
//The 'using' statement will call the Dispose() method of the combobox
return stotReason;
}
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
//This style indicates that the control should be placed directly in a cell
return UITypeEditorEditStyle.None;
}
I have already added this code in the form,
Grid.Headers[0]["column1"].Editor = new ButtonEditor();
2. Also, even I set the focusSettings.Mode = FocusMode.Cell, the I still cannot select a single grid cell, but a row selection. May I know if there are any additional settings required?
Thanks very much.
-
Hello,
1. Windows has a serious limitation in number of GDI handlers per application (about 10 000). Each control consumes a handle and of you create a real control per cell - the application will rapidly reach the limit and will crash. The single solution is to graphically emulate the control for inactive cells and create a real control when the user starts editing in the cell. The control will be destroyed when the user ends editing. Below we’ll provide an example demonstrating how to implement this feature:
internal class ButtonEditor : UITypeEditorEx
{
public override bool GetPaintCellSupported()
{
return true;
}
public override void PaintCell(PaintCellEventArgs e)
{
base.PaintCell(e);
//Do your custom painting
ControlPaint.DrawButton(e.Graphics, e.Cell.VirtualBounds, ButtonState.Normal);
}
public override StopEditReason EditCell(IGridEditorService service, Cell cell, StartEditReason reason)
{
//Create a control
Button button = new Button();
//Add desired callbacks
button.Click += delegate
{
//When the user clicks on the button - stop editing
service.CloseCellControl(StopEditReason.CloseControl);
//Set a new value here
//cell.Value = _new_value;
};
//Attach newly created control to the grid. (The control becomes visible here)
return service.CellEditControl(button, cell.VirtualBounds, reason);
}
}2. Focusing is a grid's attribute: only single row/column can be focused. Selection is a row's attribute. In other words multiple rows can be selected. Disable selection to get the wanted result:
_grid.Selection.Enabled = false;
Best regards,
Dapfor
0
Please sign in to leave a comment.
Comments
1 comment