Trind 07
Trind 07

Reputation: 919

Conditionally hide DeleteButton in GridViewCommandColumn

I have ASPxGridView to view a list of records. From the view, I create a delete column and want it to show a delete button whenever the record does have a satisfy condition. The code below:

<dx:GridViewCommandColumn Caption="Delete" VisibleIndex="1" Width="30px"
                meta:resourcekey="GridViewCommandColumnResource1">
                <DeleteButton Visible="True">
                </DeleteButton>
            </dx:GridViewCommandColumn>

So I focus on the Visible attribute of DeleteButton. The condition must return a boolean value so it knows when to show and hide the delete button. Below is an example how to implement this:

<DeleteButton Visible='<%# ShowHide(Eval("Active")) %>'>
                </DeleteButton>

in VB Code:

Protected Function ShowHide(Active As Boolean) As Boolean
    Return Active
End Function

So the function need to return a True value if Active is True, and False value if Active is False. In other words, I do trigger a function in code behind on every record to make it show the delete button if record does have a satisfy condition. But I got an error message in the end:

Parser Error Message: Databinding expressions are only supported on objects that have a DataBinding event. DevExpress.Web.ASPxGridView.GridViewCommandColumnButton does not have a DataBinding event.

I stuck there and don't know other way to do this. Please help me with this.

Upvotes: 3

Views: 1755

Answers (2)

Trind 07
Trind 07

Reputation: 919

After a night to research on this, I have find a new way to solve this issue:

This block of code from aspx file within an ASPxGridView:

<dx:GridViewCommandColumn Caption="Delete" VisibleIndex="1" Width="30px"
                meta:resourcekey="GridViewCommandColumnResource1">
                <DeleteButton Visible='True'><!--TRI - 20160329 Please make sure the Visible attribute always True-->
                </DeleteButton>
            </dx:GridViewCommandColumn>

From the code behide, I implement an initial method to handle a trigger on DeleteButton:

Protected Sub xgv_CommandButtonInitialize(sender As Object, e As DevExpress.Web.ASPxGridView.ASPxGridViewCommandButtonEventArgs) Handles xgv.CommandButtonInitialize
    If e.ButtonType = DevExpress.Web.ASPxGridView.ColumnCommandButtonType.Delete Then
        If sender.GetRowValues(e.VisibleIndex, "Active") = True Then
            e.Visible = False
        End If
    End If
End Sub

The method will handle the DeleteButton by checking the Active value on Command Button Initialize. If the Active is True, it will hide the DeleteButton and vice versa.

Upvotes: 1

Som Bhattacharyya
Som Bhattacharyya

Reputation: 4112

You could either handle the ASPxClientGridView.CustomButtonClick Event as shown here : Link or Place your custom control into GridViewDataColumn.DataItemTemplate : Link

Upvotes: 0

Related Questions