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

Receiving GridViewHyperlinkColumn Click Event

7 Answers 516 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chris Moore
Top achievements
Rank 1
Chris Moore asked on 18 Jan 2010, 07:37 PM
Try as I might, I can find no way to listen for a Click event within a GridViewHyperlink column cell. What am I missing? I am creating the columns of the grid dynamically based on metadata retrieved from a database, e.g.,

column = new GridViewHyperlinkColumn();
(column as GridViewHyperlinkColumn).ContentBinding = new Binding(columnInfo.FieldName);
column.DataMemberBinding = new Binding(columnInfo.FieldName);
column.IsFilterable = columnInfo.CanFilter;
column.Header = columnInfo.DisplayName;
column.IsEnabled = columnInfo.IsEnabled;
column.IsReadOnly = true;

What I would like to be able to do is write something like
(column as GridViewHyperlinkColumn).Clicked += MyClickHandler;
However, the Clicked event is not defined for the column type.

How can I receive the click event?

Thanks,
Chris.

7 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 19 Jan 2010, 08:13 AM
Hello Chris Moore,

You can easily subscribe for MouseDown event of the cells that belong to e certain column by using the RowLoaded event. For example, if your HyperLinkColumn is the first column you can try something like this:

public Window1()
{
    InitializeComponent();
  
    this.playersGrid.ItemsSource = Club.GetPlayers();
    this.playersGrid.RowLoaded += new System.EventHandler<RowLoadedEventArgs>(playersGrid_RowLoaded);
    this.playersGrid.RowUnloaded += new System.EventHandler<RowUnloadedEventArgs>(playersGrid_RowUnloaded);
}
  
void playersGrid_RowUnloaded(object sender, RowUnloadedEventArgs e)
{
    var row = e.Row as GridViewRow;
  
    if (row != null)
    {
        row.Cells[0].RemoveHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Window1_MouseLeftButtonDown));
    }
}
  
void playersGrid_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
{
    var row = e.Row as GridViewRow;
  
    if (row != null)
    {
        row.Cells[0].AddHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Window1_MouseLeftButtonDown), true);
    }
}
  
void Window1_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
      
}

Hope this code helps.

Since we always try to improve our product I am curious how you will use the click events for the hyperlink column. Such information might help us to introduce new functionality that all users can benefit from.

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
Chris Moore
Top achievements
Rank 1
answered on 19 Jan 2010, 01:50 PM
Thanks for the sample; I can now receive the event.

The reason that I need to handle the click event is to programmatically direct the user to another page within the application.

0
Chris Moore
Top achievements
Rank 1
answered on 20 Jan 2010, 12:53 PM
After further reflection, this doesn't completely solve the problem. In this case, the click event will be fired even though the link itself isn't click; merely clicking the cell area, which may not be completely consumed by the link, will generate the event. Is there no way to find out when the actual link is clicked?

Thanks.
0
Accepted
Pavel Pavlov
Telerik team
answered on 22 Jan 2010, 05:46 PM
Hi Chris Moore,

Let me ofer even simpier solution , that might solve the problem :

after InitializeComponent();  in the window constructor , you may subscribe to the click event of a hyperlink the following way:

 

this.AddHandler(Hyperlink.ClickEvent, new RoutedEventHandler(hyperlinkClicked));

and the handler itself :

void hyperlinkClicked(object sender, RoutedEventArgs args)
       {
           Hyperlink hyperLink = args.OriginalSource as Hyperlink; 
...perform your custom actions here..
       }


Kind regards,
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
Chris Moore
Top achievements
Rank 1
answered on 22 Jan 2010, 07:48 PM
Thanks for the tip; that worked perfectly.
0
lakshmi ravila
Top achievements
Rank 1
answered on 04 Mar 2010, 11:27 AM
Hi telerik team,

Thanks for the help.
Now i'm able to catch the hyperlink click event. Can you tell me how to initialise a usercontrol on this click.

We are using MVVM and I want to display usercontrol2.xaml from hyperlink click of radgridview.
Radgridview is in usercontrol1.xaml

Regards,
Lakshmi
0
Sathish
Top achievements
Rank 1
answered on 06 Aug 2010, 02:30 PM
Hi,
    How can accomplish the same hyperlink click if i have a child grid (Hierarchical grid) under the parent grid. Please let me know.

Thanks
Sathish.K
Tags
GridView
Asked by
Chris Moore
Top achievements
Rank 1
Answers by
Milan
Telerik team
Chris Moore
Top achievements
Rank 1
Pavel Pavlov
Telerik team
lakshmi ravila
Top achievements
Rank 1
Sathish
Top achievements
Rank 1
Share this question
or