How do I add a custom button to a DataGrid?

The ASP.NET DataGrid contains four "built-in" buttons; Select, Edit, Update, Cancel, and Delete. If you want to add your own custom button that performs its own custom action, do the following:

  1. Right-click the DataGrid in design mode and select Property Builder from the shortcut menu

  2. In the Properties dialog, select Columns in the left pane.

  3. In the Available columns list, select Button Column and then click the mover button ">" to add it to the Selected columns list.

  4. In the Text text box enter the text you want displayed on the button

  5. In the Button type combo box, select either LinkButton or PushButton depending on the style you want

  6. In the Command name text box, enter a string that can be used to identify the command you want to execute when the button is clicked at run time. For example, AddToCart:

  7. Click the OK button to close the dialog and save changes.

  8. Next, you need to add an event handler for this button. To do this, follow these language specific instructions:

    In C#:

    • Right-click the DataGrid and select Properties from the shortcut menu

    • In the Properties window, click the Events button (the lightning bolt), and then double-click the ItemCommand event. This adds an event handler to the form. For example:

      private void grdItems_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
      {		
      }

    • Add code to this method to handle the event. If your custom button was clicked, the DataGridCommandEventArgs object has a CommandName property that contains the text you entered in the Command name text box in the Properties dialog. For example:

      private void grdItems_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
      {
      	if (e.CommandName == "AddToCart")
      	{
      		// Perform processing
      	}
      }

    And in VB .NET:

    • Right-click the web form that contains the DataGrid and select View Code from the shortcut menu.

    • At the top left of the code window, select the DataGrid from the combo box.

    • At the top right of the code window, select ItemCommand from the combo box. This adds an event handler to the form. For example:

      Private Sub grdItems_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grdItems.ItemCommand
      
      End Sub

    • Add code to this method to handle the event. If your custom button was clicked, the DataGridCommandEventArgs object has a CommandName property that contains the text you entered in the Command name text box in the Properties dialog. For example:

      Private Sub grdItems_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grdItems.ItemCommand
      
              If e.CommandName = "AddToCart" Then
                  ' Perform processing
              End If
      
      End Sub


© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 12/25/04
Comment or report problem with topic