Search code examples
c#asp.nettelerikradgrid

Telerik RadGrid conditional display


This is my RadGrid code in asp.net page

<telerik:RadGrid ID="grvData" runat="server" GridLines="Horizontal" EnableEmbeddedSkins="false" CellPadding="0" BorderWidth="0px"
        Width="1000px" Height="300px" CellSpacing="0"
        OnItemCommand="grvData_ItemCommand"
        OnNeedDataSource="grvData_NeedDataSource" AllowPaging="True" AllowSorting="True"  Skin="skn_RadGridCustom"
        SkinsDir="|CurrentTheme|/" SkinsPath="|CurrentTheme|/" >
        <ClientSettings>
            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
        </ClientSettings>
        <MasterTableView PageSize="10" 
            AllowFilteringByColumn="false" AutoGenerateColumns="False" DataKeyNames="Id" AllowSorting="True">

            <Columns>
                <telerik:GridBoundColumn DataField="Id" DataType="System.Int64" ReadOnly="True" UniqueName="Id" Display="true">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="DetailId" DataType="System.Int64" ReadOnly="True" UniqueName="DetailId" Display="true">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn FilterControlAltText="Filter Reject column" UniqueName="Reject" HeaderText="Reject" Display="true">
                    <ItemTemplate>
                        <telerik:RadButton ID="btnReject" runat="server" ButtonType="StandardButton" Text="Reject" CommandName="Reject" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

How can I display Reject button with enabled/disabled mode based on column IsCancelled=1/0 from the stored procedure that is Databound to the RadGrid while loading the grid.


Solution

  • Please try with the below code snippet.

    ASPX.CS Please subscribe the "ItemDataBound" event for your radgrid.

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
            {
                    GridDataItem item = e.Item as GridDataItem;
                    RadButton btnReject = item.FindControl("btnReject") as RadButton; 
    
                    if (item.GetDataKeyValue("IsCancelled").ToString() == "1")
                    {
                        btnReject.Enabled = true;
                    }
            else
            {
                        btnReject.Enabled = false;  
            }
    
        }
    }
    

    ASPX To access the 'IsCancelled' field into C# code please assign this field as datakey.

    <MasterTableView DataKeyNames="Id,IsCancelled">
    

    Let me know if any concern.