This is a migrated thread and some comments may be shown as answers.
Getting RadDataGrid data on button click
2 Answers 50 Views
This is a migrated thread and some comments may be shown as answers.
Tayfun
Top achievements
Rank 1
Tayfun asked on 29 Mar 2019, 03:02 PM

Hello,

I have a RadDataGrid which has 2 text columns and a template column which has a switch inside it. What I'm trying to achieve is, when I click a button, that's below the grid, I want to get all the grid's data in the code behind and handle it according to the corresponding switch's status. How can I get the row data in code?  

<telerikGrid:RadDataGrid x:Name="dataGrid" AutoGenerateColumns="False">
    <telerikGrid:RadDataGrid.SelectionStyle>
        <telerikGrid:DataGridBorderStyle BackgroundColor="#ffc46d" />
    </telerikGrid:RadDataGrid.SelectionStyle>
    <telerikGrid:RadDataGrid.AlternateRowBackgroundStyle>
        <telerikGrid:DataGridBorderStyle BackgroundColor="#ffc46d" />
    </telerikGrid:RadDataGrid.AlternateRowBackgroundStyle>
    <telerikGrid:RadDataGrid.Columns>
        <telerikGrid:DataGridTextColumn SizeMode="Stretch" PropertyName="StandardCode" HeaderText="{i18n:TranslateExtension StandardCode}">
            <telerikGrid:DataGridTextColumn.HeaderStyle>
                <telerikGrid:DataGridColumnHeaderStyle
                       HorizontalTextAlignment="Center"
                       TextColor="Black"
                       TextFontAttributes="Bold"/>
            </telerikGrid:DataGridTextColumn.HeaderStyle>
            <telerikGrid:DataGridTextColumn.CellContentStyle>
                <telerikGrid:DataGridTextCellStyle
                   TextColor="Black"
                   VerticalTextAlignment="Start"
                   HorizontalTextAlignment="Start">
                </telerikGrid:DataGridTextCellStyle>
            </telerikGrid:DataGridTextColumn.CellContentStyle>
        </telerikGrid:DataGridTextColumn>
        <telerikGrid:DataGridTemplateColumn SizeMode="Fixed" Width="150" HeaderText="{i18n:TranslateExtension Status}">
            <telerikGrid:DataGridTemplateColumn.HeaderStyle>
                <telerikGrid:DataGridColumnHeaderStyle
                       HorizontalTextAlignment="Center"
                       TextColor="Black"
                       TextFontAttributes="Bold"/>
            </telerikGrid:DataGridTemplateColumn.HeaderStyle>
            <telerikGrid:DataGridTemplateColumn.CellContentTemplate>
                <DataTemplate>
                    <StackLayout HorizontalOptions="Center">
                        <Switch></Switch>
                    </StackLayout>
                </DataTemplate>
            </telerikGrid:DataGridTemplateColumn.CellContentTemplate>
        </telerikGrid:DataGridTemplateColumn>
        <telerikGrid:DataGridTextColumn SizeMode="Auto"  PropertyName="StandardName"  HeaderText="{i18n:TranslateExtension StandardName}">
            <telerikGrid:DataGridTextColumn.HeaderStyle>
                <telerikGrid:DataGridColumnHeaderStyle
                       HorizontalTextAlignment="Start"
                       TextColor="Black"
                       TextFontAttributes="Bold"/>
            </telerikGrid:DataGridTextColumn.HeaderStyle>
            <telerikGrid:DataGridTextColumn.CellContentStyle>
                <telerikGrid:DataGridTextCellStyle
                   TextColor="Black"
                   VerticalTextAlignment="Start"
                   HorizontalTextAlignment="Start"
                    FontSize="12">
                </telerikGrid:DataGridTextCellStyle>
            </telerikGrid:DataGridTextColumn.CellContentStyle>
        </telerikGrid:DataGridTextColumn>
    </telerikGrid:RadDataGrid.Columns>
</telerikGrid:RadDataGrid>

2 Answers, 1 is accepted

Sort by
0
Tayfun
Top achievements
Rank 1
answered on 29 Mar 2019, 03:08 PM
I managed to get the whole data from the grid by using GetDataView() method, now the problem is, how can I bind the switches to a property(IsSelected)?
0
Lance | Manager Technical Support
Telerik team
answered on 01 Apr 2019, 05:20 PM
Hi Tayfun,

The BindingContext of the DataTemplate in a TemplateColumn is the Row's data object, you can just directly bind to a property on the data object itself.

<telerikGrid:DataGridTemplateColumn.CellContentTemplate>
    <DataTemplate>
<!-- The BindingContext of this template is the row's data item. you can bind to any of the item's properties -->
        <StackLayout HorizontalOptions="Center">
            <Switch IsToggled="{Binding IsSelected, Mode=TwoWay}"/>
        </StackLayout>
    </DataTemplate>
</telerikGrid:DataGridTemplateColumn.CellContentTemplate>

Important Note:

Make sure the model implements INotifyPropertyChanged and the IsSelected property call OnPropertyChanged. Otherwise, you will not see the switch get updated when you programmatically change the value of IsSelected.

public class MyItem : INotifyPropertyChanged
{
    private bool isSelected;
 
    public bool IsSelected
    {
        get => isSelected;
        set
        {
            if (isSelected != value)
            {
                isSelected = value;
                OnPropertyChanged(nameof(IsSelected));
            }
        }
    }
 
 
    public event PropertyChangedEventHandler PropertyChanged;
 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}



To address your first question, if you don't need the DataGrid's filtering/grouping/sorted status, then you can just iterate over the ItemsSource itself instead of the DataView.

For example, if the ItemsSource is a List<MyItem>, you can just do this:

private void ButtonClick(object sender, EventArgs e)
{
    foreach (var myItem in dataGrid.ItemsSource as List<MyItem>)
    {
        // do what you need to do with myItem
    }
}

In that example above, you could change all of the IsSelected properties and the Switches in the TemplateColumn would immediately update.

Regards,
Lance | Technical Support Engineer, Principal
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Asked by
Tayfun
Top achievements
Rank 1
Answers by
Tayfun
Top achievements
Rank 1
Lance | Manager Technical Support
Telerik team
Share this question
or