Articles in this section
Category / Section

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

2 mins read

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

Conclusion

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

You can refer to our WinForms GridControl’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridControl documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms 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 WinForms GridControl and other WinForms components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-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)
Please sign in to leave a comment
Access denied
Access denied