2

I have an ASPxGridView control that needs a button that is visible on a conditional basis. I have searched and read several articles but have not found a fix for my particular situation. The button is called btnAllocatePlan and it should only be displayed if the value in a column called PayPlanFlg is true. Below is the markup for the grid and two things I tried in the codebehind, which is where I'd rather do this. Actually neither of these events even fired. Any help is greatly appreciated!

<dx:ASPxGridView ID="grdPayments" runat="server" CssClass="tblLined" SettingsPager-PageSize="50"
        AutoGenerateColumns="False" KeyFieldName="PaymentKey" Width="100%">
        <SettingsBehavior AllowFocusedRow="True" ProcessFocusedRowChangedOnServer="True" />
        <Columns>
            <dx:GridViewDataTextColumn Caption="CaseKey" FieldName="CaseKey" Visible="False" VisibleIndex="0">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Name" FieldName="Name" VisibleIndex="1" Width="10%">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Association" FieldName="AssociationName" VisibleIndex="2" Width="15%">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Address" FieldName="Address1" VisibleIndex="3" Width="15%">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Amount Paid" FieldName="Amount" VisibleIndex="4" Width="5%">
                <PropertiesTextEdit DisplayFormatString="c"></PropertiesTextEdit>
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Payment Date" FieldName="TransactionTime" VisibleIndex="5" Width="12%">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Payment Type" FieldName="PaymentType" VisibleIndex="6" Width="3%">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Amount Owed" FieldName="Balance" VisibleIndex="7"  Width="5%">
                <PropertiesTextEdit DisplayFormatString="c">
                </PropertiesTextEdit>
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Amount Available" FieldName="AmountAvailable" VisibleIndex="8" Width="6%">
                <PropertiesTextEdit DisplayFormatString="c">
                </PropertiesTextEdit>
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataHyperLinkColumn Name="AllocationLink" VisibleIndex="9" Caption=" " UnboundType="String" Width="5%">
                <DataItemTemplate>
                    <!--<a href="javascript:OpenPaymentAllocation()"><%#"Allocate"%></a>-->
                    <dx:ASPxButton ID="btnAllocate" runat="server" Text="Allocate" OnClick="btnAllocate_Click" />
                </DataItemTemplate>
            </dx:GridViewDataHyperLinkColumn>
            <dx:GridViewDataHyperLinkColumn Name="PlanLink" VisibleIndex="10" Caption=" " UnboundType="String" Width="5%">
                <DataItemTemplate>
                    <!--<a href="javascript:OpenPaymentPlanAllocation()"><%#"Allocate"%></a>-->
                    <dx:ASPxButton ID="btnAllocatePlan" runat="server" Text="Pay Plan" OnClick="btnAllocatePlan_Click" />
                </DataItemTemplate>
            </dx:GridViewDataHyperLinkColumn>
            <dx:GridViewDataTextColumn Caption="PayPlanFlg" FieldName="PayPlanFlg" Visible="false" VisibleIndex="11">
            </dx:GridViewDataTextColumn>
        </Columns>            
    </dx:ASPxGridView>

Codebehind attempts:

Protected Sub grdPayments_CustomButtonInitialize(sender As Object, e As ASPxGridViewCustomButtonEventArgs)

        If e.Button.ID = "btnAllocatePlan" Then

            If grdPayments.GetRowValues(e.VisibleIndex, 11, "PayPlanFlg") = 1 Then

                e.Button.Visibility = GridViewCustomButtonVisibility.Invisible

            End If

        End If

    End Sub

Protected Sub ASPxGridView1_HtmlDataCellPrepared(sender As Object, e As ASPxGridViewTableDataCellEventArgs)

        If e.DataColumn.FieldName = "ID" Then
            Dim textBox As ASPxButton = TryCast(ASPxGridView1.FindRowCellTemplateControl(e.VisibleIndex, e.DataColumn, "btn"), ASPxButton)
            If Convert.ToString(e.GetValue("ItemName")).Equals("B") Then
                textBox.Text = Convert.ToString(e.CellValue)
                textBox.Visible = True
            Else
                textBox.Visible = False
            End If
        End If

    End Sub
Mike
  • 417
  • 7
  • 28

1 Answers1

1

The best approach is to handle the Button's Init/Load event. This recommended technique is described in the The general technique of using the Init/Load event handler KB article.

Check below example code for hiding a particular button in a row..

aspx

<dx:GridViewDataHyperLinkColumn Name="AllocationLink" VisibleIndex="9" Caption=" " UnboundType="String" Width="5%">
    <DataItemTemplate>
        <!--<a href="javascript:OpenPaymentAllocation()"><%#"Allocate"%></a>-->
        <dx:ASPxButton ID="btnAllocate" runat="server" Text="Allocate"  OnInit="btnAllocate_Init" />
    </DataItemTemplate>
</dx:GridViewDataHyperLinkColumn>

.cs

protected void btnAllocate_Init(object sender, EventArgs e)
{
    DevExpress.Web.ASPxButton btn = (DevExpress.Web.ASPxButton)sender;
    GridViewDataItemTemplateContainer container = btn.NamingContainer as GridViewDataItemTemplateContainer;

    // You can remove the if statement, and try to insert a new record. You'll catch an exception, because the DataBinder returns null reference
    if (container != null &&  !container.Grid.IsNewRowEditing)
        btn.Visible = !(bool)DataBinder.Eval(container.DataItem, "PayPlanFlg");
}

Reference:
ASPxGridView - Hide the control in the DataItemTemplate conditionally

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • Thanks for your reply, Niranjan. I tried your code, but I'm getting an error that says DevExpress.Web.ASPxButton is not defined. Any ideas what's causing that? – Mike Mar 16 '17 at 19:09
  • I sorted that issue. I'm using an older version of DevExpress, when the ASPxButton was still in the ASPxEditors namespace. – Mike Mar 16 '17 at 19:40
  • but you functionality is still same.. just change then namespace and refer the code to modify it according your DevExpress version. – Niranjan Singh Mar 17 '17 at 04:42