Set a toggle State by a Datable in a Grid

1 Answer 29 Views
Grid ToggleButton
dds
Top achievements
Rank 1
Iron
Iron
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.

Thanks, 

Alvaro.

 

 

1 Answer, 1 is accepted

Sort by
0
Doncho
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">
        <Columns>
            <telerik:GridBoundColumn DataField="ID" DataType="System.Int32"
                FilterControlAltText="Filter ID column" HeaderText="ID"
                ReadOnly="True" SortExpression="ID" UniqueName="ID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Name"
                FilterControlAltText="Filter Name column" HeaderText="Name"
                SortExpression="Name" UniqueName="Name">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn HeaderText="Visibility" DataField="Visibility" UniqueName="Visibility">
                <ItemTemplate>
                    <telerik:RadToggleButton ID="RadToggleButton1" runat="server" SelectedToggleStateIndex='<%# Eval("Visibility").ToString() == "Show" ? 0 : 1 %>'>
                        <ToggleStates>
                            <telerik:ButtonToggleState Text="Show" Value="Show" />
                            <telerik:ButtonToggleState Text="Hide" Value="Hide" />
                        </ToggleStates>
                    </telerik:RadToggleButton>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

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,
Doncho
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.

Tags
Grid ToggleButton
Asked by
dds
Top achievements
Rank 1
Iron
Iron
Answers by
Doncho
Telerik team
Share this question
or