)
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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to have a combination of ComboBox and ButtonEdit cell within a single Grid cell in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: April 18, 2016 |
Last Revised Date: December 17, 2019

Combination of ComboBox and ButtonEdit cell

The combo box cell and button edit cell can be implemented by having a ComboCellModel and the ComboCellRenderer that is derived from GridComboCellModel and GridComboCellRenderer.

The below link helps to guide the custom cell renderer: Custom cell renderer .

The above link helps to learn about custom cell renderer, and we can learn how the cell models and cell renderer will be works.

Creating Cell Model

In the combo cell model, the combo cell renderer class object is invoked by a CreateRenderer() override method.

C#

#region combocell model
class ComboCellModel : GridComboBoxCellModel
{
public ComboCellModel(GridModel grid)
    : base(grid)
{
    //Set the size of the button bar.
    ButtonBarSize = new System.Drawing.Size(30, 15);
}
public override GridCellRendererBase CreateRenderer(GridControlBase control)
{
    return new ComboCellRenderer(control, this);
}
}

 

VB

#Region "combocell model"
Friend Class ComboCellModel
 Inherits GridComboBoxCellModel
Public Sub New(ByVal grid As GridModel)
 MyBase.New(grid)
 'Set the size of the button bar.
 ButtonBarSize = New System.Drawing.Size(30, 15)
End Sub
Public Overrides Function CreateRenderer(ByVal control As GridControlBase) As GridCellRendererBase
 Return New ComboCellRenderer(control, Me)
End Function
End Class
 

Creating Cell Renderer

In the combo cell renderer, the customization of the cell is achieved. The combo box button is added to ComboCellRenderer.

C#

#region Combocell cellRenderer
class ComboCellRenderer : GridComboBoxCellRenderer
{
public GridCellButton button;
public ComboCellRenderer(GridControlBase control, GridCellModelBase model)
: base(control, model)
{
button = new GridCellButton(this);
            
//Add the button to the comboBox cell
this.AddButton(button);
}
}

 

VB

#Region "Combocell cellRenderer"
Friend Class ComboCellRenderer
 Inherits GridComboBoxCellRenderer
Public button As GridCellButton
Public Sub New(ByVal control As GridControlBase, ByVal model As GridCellModelBase)
 MyBase.New(control, model)
button = New GridCellButton(Me)
 
'Add the button to the comboBox cell
Me.AddButton(button)
End Sub
 

 

Adding Cell Models

The created model (ComboCellModel) can be added into CellModels collection and the cell type name ComboBoxButtonEditCell can be defined.

C#

this.gridControl1.Model.CellModels.Add("ComboBoxButtonEditCell", new ComboCellModel(this.gridControl1.Model));

 

VB

Me.gridControl1.Model.CellModels.Add("ComboBoxButtonEditCell", New ComboCellModel(Me.gridControl1.Model))

 

Assigning Cell type

The cell type ComboBoxButtonEditCell can be assigned to a cell or range of cells using the CellType property.

C#

this.gridControl1[2, 3].CellType = "ComboBoxButtonEditCell";

 

VB

Me.gridControl1(2, 3).CellType = "ComboBoxButtonEditCell"

 

Event for the Cell Button

To trigger an event when cell button clicked, the renderer.button.Clicked event can be used. This event is coded in the cell renderer level. Likewise, any events can be added in ComboBoxButtonEditCell.

C#

ComboCellRenderer renderer = gridControl1.GetCellRenderer(2, 3) as ComboCellRenderer;
renderer.button.Clicked += button_Clicked;
                   
}
void button_Clicked(object sender, GridCellEventArgs e)
{
MessageBox.Show(String.Format("Button Edit Pressed at ({0},{1})", e.RowIndex, e.ColIndex));
}

 

VB

Private renderer As ComboCellRenderer = TryCast(gridControl1.GetCellRenderer(2, 3), ComboCellRenderer)
Private renderer.button.Clicked += AddressOf button_Clicked
 
}
Private Sub button_Clicked(ByVal sender As Object, ByVal e As GridCellEventArgs)
MessageBox.Show(String.Format("Button Edit Pressed at ({0},{1})", e.RowIndex, e.ColIndex))
End Sub

 

Screenshot

ComboBox with button in a grid cell

Samples:

C#: ComboBox with Button

VB: ComboBox with Button

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon