Set a toggle State by a Datable in a Grid

1 Answer 29 Views
Grid ToggleButton
Top achievements
Rank 1
dds asked on 15 Jun 2021, 11:25 AM

Hi there, 


I have a radtoggleButton in a grid with two states and I´m creating the new rows using a DataTable source of the Grid.

My question is how I can use the datable to set the second state of the toggle button.

This is how the grid looks like: 

And this is the column template for the grid. 

I´ve tried to set the value as newRow("Visibility") = "Hide"  or adding the button directly to this cell of the row. 

Let me know if you have some ideas, please.





1 Answer, 1 is accepted

Sort by
Telerik team
answered on 18 Jun 2021, 07:24 AM

Hi Alvaro,

One way to relate the SelectedToggleState to the data bound to the RadGrid is to use a server expression in the ToggleButton's SelectedToggleStateIndex property.

Here is a sample demonstrating the approach:

<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="ID">
            <telerik:GridBoundColumn DataField="ID" DataType="System.Int32"
                FilterControlAltText="Filter ID column" HeaderText="ID"
                ReadOnly="True" SortExpression="ID" UniqueName="ID">
            <telerik:GridBoundColumn DataField="Name"
                FilterControlAltText="Filter Name column" HeaderText="Name"
                SortExpression="Name" UniqueName="Name">
            <telerik:GridTemplateColumn HeaderText="Visibility" DataField="Visibility" UniqueName="Visibility">
                    <telerik:RadToggleButton ID="RadToggleButton1" runat="server" SelectedToggleStateIndex='<%# Eval("Visibility").ToString() == "Show" ? 0 : 1 %>'>
                            <telerik:ButtonToggleState Text="Show" Value="Show" />
                            <telerik:ButtonToggleState Text="Hide" Value="Hide" />

C# code to bind some dummy data to the RadGrid:

protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    (sender as RadGrid).DataSource = Enumerable.Range(1, 6).Select(x => new { ID = x, Name = "Name " + x, Visibility = (x % 3 == 0 ? "Show" : "Hide") });

I hope this will help you achieve the desired.

Please let me know if any questions come up.

Kind regards,
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Grid ToggleButton
Asked by
Top achievements
Rank 1
Answers by
Telerik team
Share this question