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

[Solved] Disabling GridViewRow event for header row

5 Answers 128 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.
Gilles
Top achievements
Rank 1
Gilles asked on 10 Jan 2011, 08:36 PM
Hello,

I have a RadGridView on a page. I have created an event so that when the user clicks on a row an event is called. My problem is that this event is also called when I click on a column header. So when I try to click on a column header to sort the column, the event also gets called.

Here is where I add my event handler:
this.myGrid.AddHandler(GridViewRow.MouseLeftButtonUpEvent, new MouseButtonEventHandler(this.GridViewRow_OnMouseLeftButtonUp), true);


And here is my event handler:
private void GridViewRow_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
            var gridView = sender as Telerik.Windows.Controls.GridView.GridViewDataControl;                  
              
             if (gridView.CurrentCell == null)
                return;
  
            var dossier = ((Telerik.Windows.Controls.RadRowItem)(((Telerik.Windows.Controls.GridView.GridViewCellBase)((gridView).CurrentCell)).ParentRow)).Item;  
  
            if (dossier != null)
            {
                // ...             
            }            
}

5 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 11 Jan 2011, 08:14 AM
Hi Gilles,

You may use the ParentOfType<T> extension method to find the parent of the clicked element and check if it is a GridViewRow. For example:

private void GridViewRow_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    var clickedElement = e.OriginalSource as FrameworkElement;
    if(clickedElement != null)
    {
        var row = clickedElement.ParentOfType<GridViewRow>();
        if(row != null)
        {
                     //you may implement your custom logic here.
        }
    }      
}
 

Greetings,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Gilles
Top achievements
Rank 1
answered on 11 Jan 2011, 02:32 PM
Thank you for your fast answer.
0
Gilles
Top achievements
Rank 1
answered on 11 Jan 2011, 09:33 PM
I have another question, is there a way to keep the sorting functionality when the user clicks on a header row with this code ?

Right now, when I click on a header row the event handler gets called and not the sort. If I remove the event handler I can sort in my grid.

I added e.Handled = false; if the current row is a header row but to no avail.

private void GridViewRow_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    var clickedElement = e.OriginalSource as FrameworkElement;
    if(clickedElement != null)
    {
       var headerRow = clickedElement.ParentOfType<GridViewHeaderRow>();
  
       if (headerRow != null)
       
               e.Handled = false;
               return;
       }
  
        var row = clickedElement.ParentOfType<GridViewRow>();
        if(row != null)
        {
                     //you may implement your custom logic here.
        }
    }       
}

When I debug the method, it correctly enters the if condition when a header row is clicked and e.Handled gets assigned to false. Yet, no sorting occurs.
0
Maya
Telerik team
answered on 12 Jan 2011, 09:27 AM
Hello Gilles,

I have tried to reproduce the issue you defined, but I was not able to. I am sending you the sample project I used for testing it. Please take a look at it and let me know in case of any discrepancies according to your specific settings.
 

All the best,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Gilles
Top achievements
Rank 1
answered on 13 Jan 2011, 02:37 PM
Hello,

After doing some tests this I have found my problem. I had a RadTabControl with two tabs. I have the grid with the event handler on the first tab and a similar grid without an event handler on the second tab.

The sorting didn't work on the first grid and I thought that was because of the event handler.

The actual reason was that the data both grid were bound to the same datasource which was called through an async call. When the first grid was defined the datasource was still empty and the column didn't get the sort capabalities.

I changed my code so that the column is recreated after my Observable datasource was changed and now everything works fine.

Thanks for your support.
Tags
GridView
Asked by
Gilles
Top achievements
Rank 1
Answers by
Maya
Telerik team
Gilles
Top achievements
Rank 1
Share this question
or