New to Telerik UI for WinForms? Download free 30-day trial

GridViewCommandColumn

GridViewCommandColumn displays a button element that responds to user input mouse clicks and keyboard key presses. Either mouse click or keyboard input fires the CommandCommandCellClick event. The button text can be supplied through the column FieldName property values or by the column DefaultText property. To display the same button text for every cell, set the UseDefaultText property to true and the DefaultText property to the value you want displayed in the button. UseDefaultText is false by default. GridViewCommandColumn inherits from GridViewDataColumn.

The example below creates two GridViewCommandColumns. The first has UseDefaultText set to false and so displays the FieldName value for "ProductName" in the button text. The second has the UseDefaultText property set to true and the DefaultText property set to "Order". In both cases the RadGridView.CommandCellClick reacts to a mouse click, casts "sender" to be GridCommandCellElement and makes use of the Value property.

WinForms RadGridView GridViewCommandColumn

Add GridViewCommandColumn to the grid.

public GridViewCommandColumn1()
{
    InitializeComponent();
    GridViewCommandColumn commandColumn = new GridViewCommandColumn();
    commandColumn.Name = "CommandColumn";
    commandColumn.UseDefaultText = false;
    commandColumn.FieldName = "ProductName";
    commandColumn.HeaderText = "Order";
    radGridView1.MasterTemplate.Columns.Add(commandColumn);
    GridViewCommandColumn commandColumn2 = new GridViewCommandColumn();
    commandColumn2.Name = "CommandColumn2";
    commandColumn2.UseDefaultText = true;
    commandColumn2.DefaultText = "Order";
    commandColumn2.FieldName = "ProductName";
    commandColumn2.HeaderText = "Order";
    radGridView1.MasterTemplate.Columns.Add(commandColumn2);
    radGridView1.CommandCellClick += new CommandCellClickEventHandler(radGridView1_CommandCellClick);
}
void radGridView1_CommandCellClick(object sender, EventArgs e)
{
    MessageBox.Show("You ordered " + ((sender as GridCommandCellElement)).Value);
}

Public Sub New()
    InitializeComponent()
    Dim commandColumn As New GridViewCommandColumn()
    commandColumn.Name = "CommandColumn"
    commandColumn.UseDefaultText = False
    commandColumn.FieldName = "ProductName"
    commandColumn.HeaderText = "Order"
    RadGridView1.MasterTemplate.Columns.Add(commandColumn)
    Dim commandColumn2 As New GridViewCommandColumn()
    commandColumn2.Name = "CommandColumn2"
    commandColumn2.UseDefaultText = True
    commandColumn2.DefaultText = "Order"
    commandColumn2.FieldName = "ProductName"
    commandColumn2.HeaderText = "Order"
    RadGridView1.MasterTemplate.Columns.Add(commandColumn2)
    AddHandler RadGridView1.CommandCellClick, AddressOf radGridView1_CommandCellClick
End Sub
Sub radGridView1_CommandCellClick(ByVal sender As Object, ByVal e As EventArgs)
    MessageBox.Show("You ordered " + ((TryCast(sender, GridCommandCellElement))).Value)
End Sub

Buttons Styles

You can use the Image and TextImageRelation properties of the GridViewCommandColumn in order to set the image for all buttons in the grid.

The following article shows how you can access the buttons in the CellFormating event and change their styles: Formatting GridViewCommandColumn.

See Also

In this article