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

[Solved] How to get the selected rows? not the item...

13 Answers 573 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Michele
Top achievements
Rank 2
Michele asked on 30 Dec 2009, 01:58 PM
Hello,
I need to change a lot of column based on the selection of the user, I've added a combobox in that is enabled when more than one item is selected... I've this as SelectionChanged event :

 private void gvComunicazioni_SelectionChanged(object sender, SelectionChangeEventArgs e) 
        { 
            if (e.AddedItems.Count > 0) 
            { 
                foreach (Dis.ComunicazioniObj i in e.AddedItems) 
                { 
                    if (!m_listSelectedItems.ContainsKey(i)) 
                    { 
                        MessageBox.Show(gvComunicazioni.CurrentItem.GetType().ToString()); 
                        m_listSelectedItems.Add(i, null); 
                    } 
                } 
            } 
 
            if (e.RemovedItems.Count > 0) 
            { 
                foreach (Dis.ComunicazioniObj i in e.RemovedItems) 
                { 
                    if (!m_listSelectedItems.ContainsKey(i)) 
                    { 
                        m_listSelectedItems.Remove(i); 
                    } 
                } 
            } 
 
            if (gvComunicazioni.SelectedItems.Count > 1) 
            { 
                cbMultipleStatus.IsEnabled = true
            } 
            else 
            { 
                cbMultipleStatus.IsEnabled = false
            } 
             
            
        } 

and since the Added/RemovedItems conteins the object binded to the control and not the row I decided to add this to have a matching collection object/row... this with no luck...

private Dictionary<Dis.ComunicazioniObj, GridViewRowItem> m_listSelectedItems = new Dictionary<Dis.ComunicazioniObj, GridViewRowItem>(); 

Can someone help me on this?
Thanks in advance
Paolo


13 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 04 Jan 2010, 02:02 PM
Hi Paolo,

I am not sure I understand the request. Can you please clarify in more  details the requirements so I can  be able to prepare a small example for you ?

Sincerely yours,
Pavel Pavlov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Michele
Top achievements
Rank 2
answered on 04 Jan 2010, 02:14 PM
Hello Pavel,
First of all Happy New Year!!, I try to be more accurate...
I've got a gridview that allows multiple selection and I've got a combobox outside the grid that I use to apply the selected item to the multiple selected rows...

The event gvComunicazioni_SelectionChanged has got 2 collection AddedItems and RemovedItems...that contains the data I've binded to the row of the Grid... how can I get the real row and not the DataItem?

For now I've found the workaround of keeping a idictionary<DataItem,Row> filled on RowLoaded but I thinks there's a more polite way of doing so...

Thanks

Paolo
0
Milan
Telerik team
answered on 07 Jan 2010, 10:15 AM
Hi Paolo,

Happy New Year to you too. I wish you all the best in this new year.

You can easily get the selected rows using our ItemContainerGenerator. For example:

private void RadGridView_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
    if (e.AddedItems.Count > 0)
    {
        var selectedRow = (GridViewRow)this.playersGrid.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]);
    }
}


Kind regards,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Michele
Top achievements
Rank 2
answered on 11 Jan 2010, 03:16 PM
Hello Milan,
thanks for your answer... I've tried to do as you sad on a out-of-radgridvirew combobox's SelectionChanged event, but I got a Null pointer reference exception when I cycle on the RadGridView's SelectedItem... I tried using the
gvComunicazioni.ItemContainerGenerator.ContainerFromIndex 

passing 0 as test and it gets the item.... using .ContainerFromItem does not.... is it due to virtualization and the fact that it's outside the radgridview??

Thanks

Paolo
0
Milan
Telerik team
answered on 11 Jan 2010, 03:37 PM
Hello Paolo,

You are correct - you can only get the container for an item that is in the viewport. You can work around this problem by disabling our virtualization mechanism and setting EnableRowVirtualization to false. 

Sincerely yours,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Michele
Top achievements
Rank 2
answered on 11 Jan 2010, 03:37 PM
Ok... I've searched a bit and if I turn off grouping it works... but I can't turn off grouping! how can I do that???
Thanks in advance

Paolo
0
Milan
Telerik team
answered on 11 Jan 2010, 03:47 PM
Hello Paolo,

I am sure that I understand your question. Could you elaborate on this?

Greetings,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Michele
Top achievements
Rank 2
answered on 11 Jan 2010, 03:53 PM
Hello Milan,
I just tried to set the virtualization off but with no luck, the only way I got of getting it works is disabling the grouping... meanwhile I googled a bit and I've found this post , as you can see here he can do that since it's in the InitializeComponent() and can attach/detach event... but on the selectionevent I can't.... my custom- ugly-looking solution is of keeping two

private Dictionary<Dis.ComunicazioniObj, GridViewRowItem> m_listaRow = new Dictionary<Dis.ComunicazioniObj, GridViewRowItem>();  
private Dictionary<Dis.ComunicazioniObj, GridViewRowItem> m_listaSelectedRow = new Dictionary<Dis.ComunicazioniObj, GridViewRowItem>(); 

but it consumes a lot of memory... the m_listaRow is filled in that way :

  private void gvComunicazioni_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)  
        {  
            if (e.DataElement != null && e.DataElement is Dis.ComunicazioniObj)  
            {  
                GridViewRowItem gvr = e.Row;  
                Dis.ComunicazioniObj obj = e.DataElement as Dis.ComunicazioniObj;  
                SetComboBoxValue(gvr, obj.IDStatoComunicazione);  
                m_listaRow.Add(e.DataElement as Dis.ComunicazioniObj, e.Row);  
            }  
        } 

Thanks
Paolo
0
Milan
Telerik team
answered on 11 Jan 2010, 04:21 PM
Hello Paolo,

In general I would not recommend storing rows in lists. Most probably there is a solution that does not require you to store the UI elements of the grid, Could you please share more details about the functionality that you are implementing so that we can try to provide you with a better solution.

Greetings,

Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Michele
Top achievements
Rank 2
answered on 11 Jan 2010, 04:29 PM
Hello Milan,
trust me neither I want to store row in a list, I'm aware that the object cannot be the same and it can be another one, generated (considering also that there's virtualization in the middle!), by the way I've got a gridview with 3 grouping,and this structure :

 <telerikGridView:RadGridView Grid.Row="1" DataLoadMode="Asynchronous" x:Name="gvComunicazioni" DataContext="{x:Null}" AutoGenerateColumns="False" FontSize="9" ColumnsWidthMode="Auto" ShowGroupPanel="False" ScrollMode="Deferred" RowLoaded="gvComunicazioni_RowLoaded" CanUserSelect="True" MultipleSelect="True" SelectionChanged="gvComunicazioni_SelectionChanged" GroupRowStyle="{StaticResource GridViewGroupRowStyle1}" EnableRowVirtualization="False" VirtualizingStackPanel.VirtualizationMode="Standard" > 
            <telerikGridView:RadGridView.GroupDescriptors> 
                <data:GroupDescriptor Member="FilialeDescr" SortDirection="Ascending">  
                </data:GroupDescriptor> 
                <data:GroupDescriptor Member="RMDescr" SortDirection="Ascending">  
                </data:GroupDescriptor> 
                <data:GroupDescriptor Member="NDGCliente" SortDirection="Ascending">  
                </data:GroupDescriptor> 
            </telerikGridView:RadGridView.GroupDescriptors> 
            <telerikGridView:RadGridView.Columns> 
                <telerikGridView:GridViewDataColumn IsVisible="False" x:Name="IDFiliale" DataMemberBinding="{Binding IDFiliale}"></telerikGridView:GridViewDataColumn> 
                <telerikGridView:GridViewDataColumn IsVisible="False" x:Name="FilialeDescr" DataMemberBinding="{Binding FilialeDescr}"></telerikGridView:GridViewDataColumn> 
                <telerikGridView:GridViewDataColumn IsVisible="False" x:Name="IDRM" DataMemberBinding="{Binding IDRM}"></telerikGridView:GridViewDataColumn> 
                <telerikGridView:GridViewDataColumn IsVisible="False" x:Name="RMDescrizione" DataMemberBinding="{Binding RMDescr}"></telerikGridView:GridViewDataColumn> 
                <telerikGridView:GridViewDataColumn IsVisible="False" x:Name="IDNDG" DataMemberBinding="{Binding IDNDG}"></telerikGridView:GridViewDataColumn> 
                <telerikGridView:GridViewDataColumn IsVisible="False" x:Name="Cliente" DataMemberBinding="{Binding DescrCliDescrAggreg}"></telerikGridView:GridViewDataColumn> 
                <telerikGridView:GridViewDataColumn  IsReadOnly="True" x:Name="Causa" Header="Causa" DataMemberBinding="{Binding Tipo}"></telerikGridView:GridViewDataColumn> 
                <telerikGridView:GridViewDataColumn IsReadOnly="True" x:Name="Data" TextAlignment="Center"  Header="Data" DataFormatString="{}{0:dd/MM/yyyy}" DataMemberBinding="{Binding DataComunicazione}"></telerikGridView:GridViewDataColumn> 
                <telerikGridView:GridViewDataColumn Header="Dettaglio">  
                    <telerikGridView:GridViewDataColumn.CellTemplate> 
                        <DataTemplate> 
                            <HyperlinkButton x:Name="lnkDettaglio" Content="Dettaglio" Click="lnkDettaglio_Click"></HyperlinkButton> 
                        </DataTemplate> 
                    </telerikGridView:GridViewDataColumn.CellTemplate> 
                </telerikGridView:GridViewDataColumn> 
                <telerikGridView:GridViewDataColumn Header="Stato Comunicazione">  
                    <telerikGridView:GridViewDataColumn.CellTemplate> 
                        <DataTemplate> 
                            <ComboBox x:Name="cbStatus"  SelectionChanged="cbStatus_SelectionChanged" DisplayMemberPath="Descr" > 
                            </ComboBox> 
                        </DataTemplate> 
                    </telerikGridView:GridViewDataColumn.CellTemplate> 
                </telerikGridView:GridViewDataColumn> 
            </telerikGridView:RadGridView.Columns> 
        </telerikGridView:RadGridView> 
        <telerik:RadProgressBar Grid.Row="1" x:Name="pb" Width="260" Height="30" Minimum="0" Maximum="1000" Margin="0,0,0,30" IsIndeterminate="True" Visibility="Collapsed" /> 
    </Grid> 

at the cbStatus change I update the DB, since there can be a lot of column I've added outside the grid a combobox that's enabled only when two or more row are selected... when I select the value from the dropdown (called cbMultipleSelect) I wish to take the selected rows and set the cbStatus DropDown to the cbMultipleSelect value

Thanks!
0
Milan
Telerik team
answered on 14 Jan 2010, 06:35 PM
Hello Paolo,

That should be easy. You just have to bind the selected item of your combo to a property of your data items and whenever the cbMultipleSelect value changes you will just update the corresponding properties of all selected data items.

I have prepared s simple project that demonstrates this approach. I am also using our built-in ComboBox column which makes it really easy to place a combo in RadGridView.

Hope this helps.


Greetings,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Michele
Top achievements
Rank 2
answered on 15 Jan 2010, 03:31 PM
Hello Milan,
I've tried your example but it doesn't work as I was expecting...
I've got a dropdown column inside my GridView...
here's the code

 <telerikGridView:GridViewDataColumn Header="Stato Comunicazione">     
                    <telerikGridView:GridViewDataColumn.CellTemplate>    
                        <DataTemplate>    
                            <ComboBox x:Name="cbStatus"  SelectionChanged="cbStatus_SelectionChanged" DisplayMemberPath="Descr" >    
                            </ComboBox>    
                        </DataTemplate>    
                    </telerikGridView:GridViewDataColumn.CellTemplate>    
                </telerikGridView:GridViewDataColumn>    
 

and you do it on all the items not in the selected items... how can I change the value of the dropdown inside the gridview's selectedsrow??
Thanks

Paolo
0
Milan
Telerik team
answered on 18 Jan 2010, 10:24 AM
Hi Paolo,

To update the selected items you should just change the loop in  masterCombo_SelectionChanged to:

foreach (Message item in this.radGridView.SelectedItems)
{
    item.Size = (int)combo.SelectedItem;
}

I have also updated the sample to work with a custom cell template similar to the one in your application. The first very important step to make this work in your application is to bind the SelectedItem property of your ComboBox column to the respective data item property - I guess that in your case that is the Status property.

Another very important step is to ensure that the data items implement INotifyPropertyChanged interface. Once this interface is implemented and the ComboBox is bound to the correct property you the UI will automatically be updated whenever the underlying data changes.

This gives you the ability to work with the data layer only and not worry about UI. Now when the selected value of the master combo is changed the code in the masterCombo_SelectionChanged will update the Status properties of all selected data items and as a result all combos will be updated as well.

Best wishes,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
Michele
Top achievements
Rank 2
Answers by
Pavel Pavlov
Telerik team
Michele
Top achievements
Rank 2
Milan
Telerik team
Share this question
or