Wednesday 10 August 2011

ASP DX GridView Command Column buttons per-row visibilty

The DevExpress GridView used for showing a list of data objects has a neat Command Column in which you can put a delete button, or edit button (and others).  On clicking this button the related data source event is fired, adding, deleting, or updating the required object.
Command Column showing the Delete button


<dx:ASPxGridView ID="GridView1" runat="server" KeyFieldName="DataObjectId" Settings-ShowFilterRow="true" />
    <Columns>
        <dx:GridViewCommandColumn ButtonType="Image">
            <ClearFilterButton Image-Url="~/Images/Icons/clear_filter.png" Visible="True" />
            <DeleteButton Image-Url="~/Images/Icons/delete.png" Visible="true" />
        </dx:GridViewCommandColumn>
...
    </Columns>
</dx:ASPxGridView>

You can hide the entire command column, or the entire column of delete buttons in your code-behind like so:
GridView1.GetCommandColumn().DeleteButton.Visible = false;
(where GetCommandColumn is a custom extention that searches for the GridViewCommandColumn).

And all the delete buttons are now invisible!

But what if you want to hide a button per-row?  Lets say you only wanted to be able to delete Scheduled events, and not Completed ones.  You could use the OnHtmlRowCreated event, then find the column index of the command column, then guess at the contorl index of the delete button, like this:
e.Row.Cells[indexColumn].Controls[0].Visible = someTest();

Or you can use a new (since 2009) event called OnCommandButtonInitialize:

<dx:ASPxGridView ID="GridView1" runat="server" KeyFieldName="DataObjectId" Settings-ShowFilterRow="true"
    OnCommandButtonInitialize="GridView1_OnCommandButtonInitialize"/>
    <Columns>
        <dx:GridViewCommandColumn ButtonType="Image">
            <ClearFilterButton Image-Url="~/Images/Icons/clear_filter.png" Visible="True" />
            <DeleteButton Image-Url="~/Images/Icons/delete.png" Visible="true" />
        </dx:GridViewCommandColumn>
...
    </Columns>
</dx:ASPxGridView>

Then create the function in your code:
protected void grdClientReviewsList_OnCommandButtonInitialize(object sender, ASPxGridViewCommandButtonEventArgs e)
        {
            if (e.ButtonType != ColumnCommandButtonType.Delete) return;
            var yourDataObject = (yourDataObjectType)GridView1.GetRow(e.VisibleIndex);
            e.Visible = yourDataObject.Status != StatusType.Completed;
        }

And now you have per-row visibility on command column buttons!
Gridview shoing selective Delete button
See community.devexpress.com for more.


Please go here to post comments.

No comments:

 
Copyright 2009 Another Blog. Powered by Blogger Blogger Templates create by Deluxe Templates. WP by Masterplan