Example E366
Visible to All Users

Grid View for ASP.NET Web Forms - How to customize command buttons in individual rows

This example demonstrates how to hide or disable a command item (button or checkbox) in the grid's command column.

A grid with command buttons

To do this, handle the CommandButtonInitialize event. This event is raised for each built-in command button. In the event handler, use the e.ButtonType property to determine the button type. Then, depending on the ButtonType property value, use the e.Visible to hide the button or use the e.Enabled property to disable the checkbox.

C#
protected void ASPxGridView1_CommandButtonInitialize(object sender, DevExpress.Web.ASPxGridViewCommandButtonEventArgs e) { bool isOddRow = e.VisibleIndex % 2 == 0; if(isOddRow) { // some condition // hide the Edit button if(e.ButtonType == DevExpress.Web.ColumnCommandButtonType.Edit) e.Visible = false; // disable the selection checkbox if(e.ButtonType == DevExpress.Web.ColumnCommandButtonType.SelectCheckbox) e.Enabled = false; } }

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

HideCommandButtons/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HideCommandButtons._Default" %> <%@ Register Assembly="DevExpress.Web.v24.2, Version=24.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dxe" %> <%@ Register Assembly="DevExpress.Web.v24.2, Version=24.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dxwgv" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" KeyFieldName="ID" OnCommandButtonInitialize="ASPxGridView1_CommandButtonInitialize"> <Columns> <dxwgv:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0" ButtonType="Button" ShowEditButton="true" ShowDeleteButton="true"> </dxwgv:GridViewCommandColumn> <dxwgv:GridViewDataTextColumn FieldName="ID" VisibleIndex="1"> </dxwgv:GridViewDataTextColumn> </Columns> </dxwgv:ASPxGridView> </div> </form> </body> </html>
HideCommandButtons/Default.aspx.cs
C#
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; namespace HideCommandButtons { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ASPxGridView1.DataSource = GetData(); ASPxGridView1.DataBind(); } private DataTable GetData() { DataTable table = new DataTable(); table.Columns.Add("ID", typeof(int)); for(int i = 0; i < 10; i++) { table.Rows.Add(i); } return table; } protected void ASPxGridView1_CommandButtonInitialize(object sender, DevExpress.Web.ASPxGridViewCommandButtonEventArgs e) { bool isOddRow = e.VisibleIndex % 2 == 0; if(isOddRow) { // some condition // hide the Edit button if(e.ButtonType == DevExpress.Web.ColumnCommandButtonType.Edit) e.Visible = false; // disable the selction checkbox if(e.ButtonType == DevExpress.Web.ColumnCommandButtonType.SelectCheckbox) e.Enabled = false; } } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.