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

How to add event handlers for sub-grids?

16 Answers 500 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lynne
Top achievements
Rank 1
Lynne asked on 06 Sep 2011, 07:06 PM
I have a three-level hierarchical grid that shows Matches/Teams/Players.  The display works fine.  
Now I'm trying to drag-and-drop items onto that grid and need to know which row I'm over when the drop occurs.

Per some of your examples, I've added an event handler for the RowLoaded/Unloaded events in which I add event handlers for MouseEnter/Leave for each row.  This works fine for the top (Game) level of the grid, and second (Teams) level, but when I add any event handlers for the Players grid, I'm having problems.

If all three levels have event handlers, when I attempt to expand the top-level grid, I get an exception as shown below.  This happens regardless of whether I have separate event handlers for each level, or a shared/generic handler.  As soon as I remove the event handlers (RowLoaded in this code snippet) in my third-level child grid, everything's fine, except of course that my application doesn't work. :)

Baffled and stuck.  

-Lynne W

Example of the XAML with event handlers for RowLoaded/Unloaded.  Same thing happens for MouseEnter/Leave.  This causes the exception shown at the end of this message.
<telerik:RadGridView Name="gridMatchPlayers"
                                         AutoGenerateColumns="False"
                                         ItemsSource="{Binding MatchPlayers}"
                                         ShowGroupPanel="False"
                                         RowLoaded="MatchPlayerGrid_RowLoaded"
                                         RowUnloaded="MatchPlayerGrid_RowUnloaded"
                                         telerikDragDrop:RadDragAndDropManager.AllowDrag="False"
                                         telerikDragDrop:RadDragAndDropManager.AllowDrop="True">

Event handling code for Player row event handlers
  • Note that I have the add'l MouseEnter/Leave event handlers commented out.  I've tried Row and Mouse event handlers for my Player (third-level) grid and they cause the exception seen below.
////////////////////////////////////////////////////////////////////////////////
//     //
//  //////  MatchPlayer grid
//   //
//
void MatchPlayerGrid_RowLoaded(Object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e) {
    var row = e.Row as GridViewRow;
    if (row != null) {
        //row.MouseEnter += new MouseEventHandler(MatchPlayerGridRow_MouseEnter);
        //row.MouseLeave += new MouseEventHandler(MatchPlayerGridRow_MouseLeave);
     }
}
 
void MatchPlayerGrid_RowUnloaded(object sender, RowUnloadedEventArgs e) {
    var row = e.Row as GridViewRow;
    if (row != null) {
        //row.MouseEnter += new MouseEventHandler(MatchPlayerGridRow_MouseEnter);
        //row.MouseLeave += new MouseEventHandler(MatchPlayerGridRow_MouseLeave);
    }
}
 
 
void MatchPlayerGridRow_MouseEnter(Object sender, System.Windows.Input.MouseEventArgs e) {
    var senderElement = e.OriginalSource as FrameworkElement;
    var mouseOverRow = senderElement.ParentOfType<GridViewRow>();
}
 
void MatchPlayerGridRow_MouseLeave(Object sender, MouseEventArgs e) {
    var senderElement = e.OriginalSource as FrameworkElement;
    var mouseExitRow = senderElement.ParentOfType<GridViewRow>();
}


Exception thrown when a third-level child grid has extant/active event handlers

System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  Source=ccm3
  StackTrace:
       at ccm3.ContentControls.ucMatchManagement.System.Windows.Markup.IStyleConnector.Connect(Int32 connectionId, Object target) in f:\PredatorGames\src\ccm3\trunk\ccm3\ContentControls\ucMatchManagement.xaml:line 245
       at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
       at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
       at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
       at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
       at System.Windows.FrameworkTemplate.LoadContent()
       at Telerik.Windows.Controls.GridView.GridViewRow.PopulateHierarchyContent() in c:\TB\102\WPF_Scrum\Release_WPF_40\Sources\Development\Controls\GridView\GridView\GridView\Rows\GridViewRow.cs:line 304
       at Telerik.Windows.Controls.GridView.GridViewRow.OnIsExpandedChanged(Boolean oldValue, Boolean newValue) in c:\TB\102\WPF_Scrum\Release_WPF_40\Sources\Development\Controls\GridView\GridView\GridView\Rows\GridViewRow.cs:line 230
       at Telerik.Windows.Controls.GridView.GridViewRow.OnIsExpandedChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) in c:\TB\102\WPF_Scrum\Release_WPF_40\Sources\Development\Controls\GridView\GridView\GridView\Rows\GridViewRow.cs:line 205
       at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
       at MS.Internal.Data.PropertyPathWorker.SetValue(Object item, Object value)
       at MS.Internal.Data.ClrBindingWorker.UpdateValue(Object value)
       at System.Windows.Data.BindingExpression.UpdateSource(Object value)
  InnerException: 

16 Answers, 1 is accepted

Sort by
0
Lynne
Top achievements
Rank 1
answered on 06 Sep 2011, 09:17 PM
Ah-hah!  I found the source of the problem, if not the solution.  In my lowest level sub-grid, I display a single column, which is a lookup of a player's name.

<telerik:RadGridView.Columns>
            <telerik:GridViewComboBoxColumn Name="colPlayerCodename"
                    DataMemberBinding="{Binding Player_PlayerId}"
                    DisplayMemberPath="Codename"
                    ItemsSource="{Binding Source={StaticResource cviewPlayers}}"
                    SelectedValueMemberPath="PlayerId" />
</telerik:RadGridView.Columns>

If I auto-generate the columns, and I have event handlers, everything's fine.  If I turn off auto-generate and only have the column specified above, the first attempt to expand the parent grid fails.  This column works fine, otherwise, so far as I can tell.
0
Lynne
Top achievements
Rank 1
answered on 06 Sep 2011, 10:09 PM
The offending line seems to be
ItemsSource="{Binding Source={StaticResource cviewPlayers}}"

If I leave out that line, and I have event handlers in the Players (third level grid), then the lookup doesn't work, but the app doesn't throw an exception.

If I put in that line, and I have event handlers, then the app throws the aforementioned exception.

If I put in that line, and I don't have event handlers, players' names are looked up and displayed correctly.

I guess my question must be, "What's wrong with my ItemsSource?"


0
Maya
Telerik team
answered on 08 Sep 2011, 09:50 AM
Hello Lynne,

It seems you have discovered a quite specific bug, when you have a GridViewComboBoxColumn in the third level of hierarchy and the ItemsSource is set with StaticResource. Please excuse us for the inconvenience cause, I have updated your Telerik points accordingly.
While we investigate the issue, I may suggest you as a possible workaround to set the ItemsSource in the code-behind during the Loaded event of the innermost grid. For example;

private void RadGridView_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
        {
            ((sender as RadGridView).Columns[1] as GridViewComboBoxColumn).ItemsSource = (this.Resources["MyViewModel"] as MyViewModel).Countries;
        }

Again, please accept our apology for the inconvenience. I will get back to you once we have more information on the issue.
 

Greetings,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Lynne
Top achievements
Rank 1
answered on 08 Sep 2011, 04:18 PM
FWIW, the exception shows up at Line 304 in GridViewRow.cs
this.HierarchyChildPresenter.Content = template.LoadContent();

with this stack
    [External Code]
>   Telerik.Windows.Controls.GridView.dll!Telerik.Windows.Controls.GridView.GridViewRow.PopulateHierarchyContent() Line 304 + 0x16 bytes    C#
    Telerik.Windows.Controls.GridView.dll!Telerik.Windows.Controls.GridView.GridViewRow.OnIsExpandedChanged(bool oldValue, bool newValue) Line 232  C#
    Telerik.Windows.Controls.GridView.dll!Telerik.Windows.Controls.GridView.GridViewRow.OnIsExpandedChanged(System.Windows.DependencyObject dependencyObject, System.Windows.DependencyPropertyChangedEventArgs e) Line 207 C#
    [External Code]
0
Lynne
Top achievements
Rank 1
answered on 08 Sep 2011, 04:41 PM
Thank you for the suggested workaround.  It's a bit cumbersome, but fully functional.  In my XAML (for the innermost (Player) grid), I've completed removed the line where the "ItemsSource" property is set and moved it to the RowLoaded event.  I'm using a CollectionViewSource, so my source differs a little from yours.

((sender as RadGridView).Columns[0] as GridViewComboBoxColumn).ItemsSource = cviewPlayers.View;

Thanks very much for the assistance.
0
Lynne
Top achievements
Rank 1
answered on 08 Sep 2011, 11:25 PM
Ouch. I found another way to get this same bug for what seems to be any combobox lookup column.

This time I was trying to auto-expand a row when it's a drop target, as in a DropInfoEvent.

Your proposed workaround, while functional, is going to be very, very difficult to sell to my boss and colleagues if I have to hard-code into the dozen places in this app where we use lookup comboboxes.  Hope there's good news soon.  Thanks.

GridViewRow gvr = e.Options.Destination as GridViewRow;
if (gvr.IsExpandable && !gvr.IsExpanded) {
    gvr.IsExpanded = true// 
}
0
Pavel Pavlov
Telerik team
answered on 14 Sep 2011, 09:29 AM
Hello Lynne,

I am afraid the workaround proposed is as close as we can get with the current version.
We are doing our best to provide a stable fix either for the service pack or for one of the next internal builds.
I am leaving the  thread open so I can notify you once we have an advance on this one.

Greetings,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Lynne
Top achievements
Rank 1
answered on 14 Sep 2011, 02:39 PM
Bugs make bunny cry. :( 

Thank you very much for the update.  My current fixes (based on your suggestions) are workable, if ugly.  Looking forward to a fix, since I really, really like the hierarchical grids.  Definitely a great way to clean up my UI.

-LW
0
Maya
Telerik team
answered on 14 Sep 2011, 03:34 PM
Hi Lynne,

Actually, after careful investigation on the problem, it turns out it has nothing to do with RadGridView or any Telerik component. It is one of the Framework. We have made just a simple test with ListBox-es only, a StaticResource involved and a handler added (I am attaching the project so that you may test it yourself). 
Thus, unfortunately, there is nothing we can do and the workaround suggested is the way to go.
 

Greetings,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Lynne
Top achievements
Rank 1
answered on 14 Sep 2011, 04:01 PM
Which "Framework"?  Entity Framework? .Net?  

Thanks for the update, though.

-LW
0
Lynne
Top achievements
Rank 1
answered on 09 Dec 2011, 05:26 PM
Still waiting for a reply, please
0
Lynne
Top achievements
Rank 1
answered on 09 Dec 2011, 05:26 PM
Still waiting for a reply, please
0
Lynne
Top achievements
Rank 1
answered on 09 Dec 2011, 05:27 PM
Still need a reply to my question(s), please.
0
Lynne
Top achievements
Rank 1
answered on 09 Dec 2011, 05:28 PM
sorry for the multiple posts, but I keep getting some kind of 'server error' page after I submit the reply
0
Lynne
Top achievements
Rank 1
answered on 09 Dec 2011, 05:28 PM
sorry for the multiple posts, but I keep getting some kind of 'server error' page after I submit the reply

(like now)
0
Maya
Telerik team
answered on 12 Dec 2011, 07:54 AM
Hello Lynne,

This is .NET 4.0 related issue. You can find more information about it here.
 

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Lynne
Top achievements
Rank 1
Answers by
Lynne
Top achievements
Rank 1
Maya
Telerik team
Lynne
Top achievements
Rank 1
Pavel Pavlov
Telerik team
Share this question
or