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

SelectedItem is null

1 Answer 58 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Don
Top achievements
Rank 1
Don asked on 13 Nov 2009, 01:18 AM
I have a grid that is bound to data objects. When I look at the grid during an event handler for a button the SelectedItem property is null even though I have selected one in the grid. According to the documentation this should be filled out with the row that is selected. I saw an earlier post that said I needed to use the CurrentRecord property. Is this still the case? This seems like a convoluted way to get to the data that is in the selected row. I am not using AutoGenerate columns but I don't see where that should make any difference.

Below is the XAML for the grid and a code snippet.

                <telerikGridView:RadGridView x:Name="grdSwimLanes" Grid.Row="2" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserFreezeColumns="False" CanUserInsertRows="False"  IsFilteringAllowed="False" ShowGroupPanel="False" RowLoaded="grdSwimLanes_RowLoaded" SelectionMode="Single" RowIndicatorVisibility="Collapsed" > 
                        <telerikGridView:RadGridView.Columns> 
                <telerikGridView:GridViewDataColumn DataMemberBinding="{Binding Description, Mode=TwoWay}" Header="Description"/> 
                        </telerikGridView:RadGridView.Columns> 
                </telerikGridView:RadGridView> 

This always displays the message box.

        private void RemoveButton_Click(object sender, RoutedEventArgs e) 
        { 
            if (grdSwimLanes.SelectedItems.Count < 1) 
            { 
                MessageBox.Show("Please select a SwimLane first."); 
                return
            } 
            SwimLaneDefinition sld = (SwimLaneDefinition) grdSwimLanes.SelectedItem; 
            Lanes.Remove(sld); 
            grdSwimLanes.Rebind(); 
        } 

We are using the latest (2009 Q3) release.

Don Miller



1 Answer, 1 is accepted

Sort by
0
Accepted
Milan
Telerik team
answered on 13 Nov 2009, 09:07 AM
Hello Don,

When a button on a row is clicked its parent row will not be selected since the button will handle the mouse click event and our selection mechanism will not kick in. For such scenarios you could try the following workaround:

private void RemoveButton_Click(object sender, RoutedEventArgs e)
{
    // required namespace Telerik.Windows.Controls.
    var button = (Button)sender;
    var parentRow = button.ParentOfType<GridViewRow>();
    var itemToRemove = parentRow.Item;
  
    Lanes.Remove(itemToRemove);
    grdSwimLanes.Rebind();
}

On the other hand, if the row is selected before the button is clicked you should be able to get the selected item in the event handler. If the SelectedItem is null in this scenario you might have found a bug and we will really appreciate it if you could send us a project that will help us reproduce the problem.


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.
Tags
GridView
Asked by
Don
Top achievements
Rank 1
Answers by
Milan
Telerik team
Share this question
or