Articles in this section

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

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 link below helps to guide the custom cell renderer
 Custom cell renderer .

 

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

 

Creating Cell Model

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

#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);
}
}
#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 the ComboCellRenderer.

#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);
}
} 
#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 to the CellModels collection and the cell type name ComboBoxButtonEditCell can be defined.

this.gridControl1.Model.CellModels.Add("ComboBoxButtonEditCell", new ComboCellModel(this.gridControl1.Model));
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.

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

Event for the Cell Button

 

To trigger an event when the cell button is 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 the ComboBoxButtonEditCell.

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));
}
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 

The output is as follows

ComboBox with button in a grid cell

Samples:

C#: ComboBox with Button

VB: ComboBox with Button


Conclusion


I hope you enjoyed learning about how to have a combination of ComboBox and ButtonEdit cells within a single Grid cell in WinForms GridControl.


You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations and WinForms GridControl documentation, and how to quickly get started for configuration specifications.


For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.


If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied