Add selected lines from Gridcontrol to Observablecollection

Asked

Viewed 153 times

2

I am trying to add my grid items to my Observablecollection but I am unsuccessful. I have a column that has the Checkbox that I put through the property ShowCheckBoxSelectorColumn.

I have the following Xaml:

<dxg:GridControl x:Name="grid" EnableSmartColumnsGeneration="True" AutoGenerateColumns="None"
                     FontWeight="Normal" SelectionMode="MultipleRow" Margin="0,0,0,324" SelectionChanged="grid_SelectionChanged">
        <dxg:GridControl.View>
            <dxg:TableView x:Name="view" ShowGroupPanel="True" AllowEditing="False" ShowTotalSummary="False" AutoWidth="True" NavigationStyle="Row"
                           ShowSearchPanelMode="Never" UseLightweightTemplates="All" FontSize="11" ShowCheckBoxSelectorColumn="True"/>
        </dxg:GridControl.View>
        <dxg:GridControl.Columns>
            <dxg:GridColumn FieldName="Codigo" Header="Código" Width="25"/>
            <dxg:GridColumn FieldName="Descricao" Header="Descrição"/>
        </dxg:GridControl.Columns>

    </dxg:GridControl>

The problem is I don’t know how to add these selected items to a collection. Someone can help me?

Grateful!

1 answer

1


This gets a little more complicated because you are using Devexpress the controls are not the same as the normal XAML controls. But looking at the documentation, it looks like it has a property called "Selecteditems" in Gridcontrol. Then in an event (like Selectionchanged for example) you can take the selected items in the "Selecteditems" property and add them to the collection for yourself. There won’t be anything simple that does this automatically. Like, something like:

public void grid_SelectionChanged(object sender, GridSelectionChangedEventArgs args) 
{
    collection.Clear(); // <-- Seu ObservableCollection
    foreach (var item in grid.SelectedItems) // SelectedItems vem do GridControl (eu acho)
    {
        // Depois de Clear(), adicionar os que são selecionados atualmente
        collection.Add(item); 
    }
}

Documentation: https://documentation.devexpress.com/#WPF/clsDevExpressXpfGridGridControltopic

  • Perfect Samuel, it worked. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.