Blogs

Paul Kimmel's Blog

Hiding and Showing a Custom Command Button in ASPxGridView

     

There is a query in our knowledgebase about hiding or showing a custom command button in an ASPxGridView ID Q241050. This blog walks you through adding custom command buttons and using the ASPxGridView’s CustomButtonInitialize to hide or show custom command buttons in the grid. the key is to use the multi-state variable DevExpress.Web.ASPxClasses.DefaultBoolean.False instead of just False for example to set the visibility of the button. Here are the steps for adding the custom command button, followed by the ASPX in Listing 1 and the code-behind in Listing 2. (You can use any datasource you want for the example; I used the Northwind database.)

1. Add an ASPxGridView to a web form
2. Use the smart tags option to choose a data source and configure the Northwind.Customers table
3. After the database is configured click the smart tags menu a second time and click the Columns editor
4. Click the Add an item button and select Command Column. (I Moved the Command Column to the 0th index position.)
5. Add some buttons to the column; use the <CustomButtons> tag to define custom buttons. Here is the markup and its shown in place in Listing 1.

<dxwgv:GridViewCommandColumn VisibleIndex="0">
                 <EditButton Visible="True" />
                 <NewButton Visible="True" />
                 <CustomButtons>
                     <dxwgv:GridViewCommandColumnCustomButton Text="Create a Copy" ID="Copy" />
                 </CustomButtons>

6. Open the Properties window for the ASPxGridView and generate a CustomButtonInitialize event for the grid
7. Add the code-behind shown in Listing 2 to set the visibility based on some criteria.

In the example all CustomerIDs starting with “A” change the command button with the ID=”Copy” visibility property to False. The key is to use the three-state DevExpress.Web.ASPxClasses.DefaultBoolean type to set the Visible property of the event arg named e. You can use any criteria you want for the solution just remember to use DefaultBoolean to set the e.Visible property.

image
Figure 1: Add a custom command button using the Columns editor form.

Listing 1: The ASPX for the sample, containing custom command buttons.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register assembly="DevExpress.Web.ASPxGridView.v9.2, Version=9.2.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxGridView" tagprefix="dxwgv" %>
<%@ Register assembly="DevExpress.Web.ASPxEditors.v9.2, Version=9.2.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxEditors" tagprefix="dxe" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <dxwgv:ASPxGridView ID="ASPxGridView1" runat="server"
        AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
        KeyFieldName="CustomerID">
        <Columns>
            <dxwgv:GridViewCommandColumn VisibleIndex="0">
                 <EditButton Visible="True" />
                 <NewButton Visible="True" />
                 <CustomButtons>
                     <dxwgv:GridViewCommandColumnCustomButton Text="Create a Copy" ID="Copy" />
                 </CustomButtons>
          </dxwgv:GridViewCommandColumn>
          <dxwgv:GridViewDataTextColumn FieldName="CustomerID" ReadOnly="True"
            VisibleIndex="1">
          </dxwgv:GridViewDataTextColumn>
          <dxwgv:GridViewDataTextColumn FieldName="CompanyName" VisibleIndex="2">
          </dxwgv:GridViewDataTextColumn>
          <dxwgv:GridViewDataTextColumn FieldName="ContactName" VisibleIndex="3">
          </dxwgv:GridViewDataTextColumn>
          <dxwgv:GridViewDataTextColumn FieldName="ContactTitle" VisibleIndex="4">
          </dxwgv:GridViewDataTextColumn>
          <dxwgv:GridViewDataTextColumn FieldName="Address" VisibleIndex="5">
          </dxwgv:GridViewDataTextColumn>
          <dxwgv:GridViewDataTextColumn FieldName="City" VisibleIndex="6">
          </dxwgv:GridViewDataTextColumn>
          <dxwgv:GridViewDataTextColumn FieldName="Region" VisibleIndex="7">
          </dxwgv:GridViewDataTextColumn>
          <dxwgv:GridViewDataTextColumn FieldName="PostalCode" VisibleIndex="8">
          </dxwgv:GridViewDataTextColumn>
          <dxwgv:GridViewDataTextColumn FieldName="Country" VisibleIndex="9">
          </dxwgv:GridViewDataTextColumn>
          <dxwgv:GridViewDataTextColumn FieldName="Phone" VisibleIndex="10">
          </dxwgv:GridViewDataTextColumn>
          <dxwgv:GridViewDataTextColumn FieldName="Fax" VisibleIndex="11">
          </dxwgv:GridViewDataTextColumn>
        </Columns>
      </dxwgv:ASPxGridView>
      <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT * FROM [Customers]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Listing 2: The code-behind sets the Visible property; in this example hiding buttons for CustomerIDs start with “A”.

Partial Class _Default
  Inherits System.Web.UI.Page

  Protected Sub ASPxGridView1_CustomButtonInitialize(ByVal sender As Object, _
    ByVal e As DevExpress.Web.ASPxGridView.ASPxGridViewCustomButtonEventArgs) _
    Handles ASPxGridView1.CustomButtonInitialize

    Dim val As Object = ASPxGridView1.GetRowValues(e.VisibleIndex, _
                                                   "CustomerID")

    If (val(0) = "A") Then
      If (e.ButtonID = "Copy") Then
        e.Visible = DevExpress.Web.ASPxClasses.DefaultBoolean.False

      End If
    End If

  End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Load

  End Sub
End Class

Published Dec 16 2009, 07:52 PM by Paul Kimmel (DevExpress)
Bookmark and Share

Comments

No Comments
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.