asp.net.ph

Skip Navigation Links

Adding Button Columns to a DataGrid Control

Controls You Can Use on Web Forms   ASP.NET Data Controls   DataGrid Control


A ButtonColumn in a DataGrid control enables authors to define custom functionality for items in the grid beyond the select, edit, delete, and hyperlink features of other column types. Using the ButtonColumn, an author can include custom-defined buttons, such as an "Add to shopping cart" button.

A ButtonColumn can have one of the following button types:

Button Type Description
LinkButton Renders a column of link buttons.
PushButton Renders a column of push buttons.

The button captions can either be static text or text read from a database.

To add a button column

  1. Set the DataGrid control’s AutoGenerateColumns property to false.
  2. Optionally assign the handler ( if there is one ) that will receive control when any of the buttons in the ButtonColumn is clicked ( in effect, when the DataGrid's ItemCommand event occurs ).
    <asp:datagrid id="myGrid" runat="server"
       autogeneratecolumns=false
       ...
       onItemCommand="updateCart"
    >
  3. Within the DataGrid declaration, declare a <Columns> element.
  4. Within the Columns element, define each of the <asp:ButtonColumn> controls you intend to display, specifying at the very least the following:
    1. the ButtonType property ( optional for link buttons, required for push and image buttons ).
    2. the CommandName for each button ( required for all button types ).
    3. the Text or DataTextField property ( required for link or push buttons ).
  5. Optionally set the ButtonColumn control’s other properties. For syntax, see DataGrid Control Syntax.

The following examples show several ways of using a ButtonColumn to display a column of command buttons in a DataGrid control.

ButtonColumn ButtonType Example
Run Sample | View Source
Master~Details Using DataGrids
Run Sample | View Source

For additional information, see ButtonColumn in the class library.

See Also

Adding Bound Columns to a DataGrid Control   Adding Hyperlink Columns to a DataGrid Control   Adding Template Columns to a DataGrid Control



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note