This is a migrated thread and some comments may be shown as answers.

RadGrid Button Column Disable Buttons not in selected row.

2 Answers 285 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eli
Top achievements
Rank 2
Eli asked on 15 Feb 2019, 02:46 AM

i have a radgrid that i have a buttoncolumn applied. the buttons work fine but i was curious if it is possible to only enable the button for the selected row?

If no rows are selected all buttons are shown, but disabled. when i click on a row the button on that row becomes enabled, and if i select another row, the button on the row i was on becomes disabled and the new row button is enabled?

i hope i am clear on this, im still very new to the telerik controls and c#. i have not been able to find anything on this matter.

 

Thanks!

2 Answers, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 19 Feb 2019, 05:24 PM
Hi Elisha,

One way of achieving that would be on server-side. Here is a short demo video: RadGrid enable button for selected row

To do that, set the ClientSettings-Selecting-AllowRowSelect and ClientSettings-EnablePostBackOnRowClick properties to True. (marked with yellow). These properties will enable selecting rows as well as do a post back if they are clicked.

Assuming that there is a GridButtonColumn as well as a GridTemplateColumn, both with buttons (marked with blue). Wire up the PreRender event for the RadGrid. (marked with green)

RadGrid Markup:

<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource" Skin="Bootstrap"
    OnPreRender="RadGrid1_PreRender">
    <ClientSettings EnablePostBackOnRowClick="true">
        <Selecting AllowRowSelect="true" />
    </ClientSettings>
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID">
        <Columns>
            <telerik:GridButtonColumn Text="Button" UniqueName="ButtonColumn" ButtonType="PushButton">
            </telerik:GridButtonColumn>
            <telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Template Column">
                <ItemTemplate>
                    <telerik:RadButton ID="RadButton1" runat="server" Text="RadButton" Enabled="false"></telerik:RadButton>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>


C# - Code behind.

In the PreRender event handler loop through all the grid items, access the Buttons of each control by following the instructions from Accessing Cells and Rows article, and set their Enabled property according to the item's selected state.

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.Items)
    {
        var btn = item["ButtonColumn"].Controls[0] as Button;
        if (btn != null)
            btn.Enabled = item.Selected;
 
        var rBtn = item["TemplateColumn"].FindControl("RadButton1") as RadButton;
        if (rBtn != null)
            rBtn.Enabled = item.Selected;
    }
}

If in case, you would like to do this on client-side, please let us know. That can be done too, in a slightly different way.

Kind regards,
Attila Antal
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Eli
Top achievements
Rank 2
answered on 21 Feb 2019, 05:49 PM

This is Perfect!! it is exactly what i needed and has taught me something new. and I have already started modifying it to better suit my project.

I greatly appreciate the help on this!

Tags
Grid
Asked by
Eli
Top achievements
Rank 2
Answers by
Attila Antal
Telerik team
Eli
Top achievements
Rank 2
Share this question
or