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

RadGridView - ToolTip for Dynamically Created Column

3 Answers 584 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 02 May 2018, 07:45 PM

Hi - 

I have a RadGridView that populates dynamically based on columns present in an Excel spread-sheet. I'm able to create the columns but not having luck getting the ToolTip to display. 

Thanks in advance - Jon

Here is my code snippet: 

               foreach (DataColumn column in dt.Columns)
                {
                    Telerik.Windows.Controls.GridViewDataColumn gridviewDataColumn = new Telerik.Windows.Controls.GridViewDataColumn
                    {
                        Header = column.ColumnName,
                        Name = column.ColumnName,
                        DataMemberBinding = new System.Windows.Data.Binding(column.ColumnName),
                        // Tooltip = ??? 
                    };

                    dtg_PreviewMapGrid.Columns.Add(gridviewDataColumn);
                }
                dtg_PreviewMapGrid.ItemsSource = dt;

 

3 Answers, 1 is accepted

Sort by
0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 07 May 2018, 12:31 PM
Hi Jon,

I am assuming that you want to set ToolTip to the cell. What I can suggest you is to subscribe to the MouseEnter and MouseLeave events of the GridViewCell. In the mouse enter event handler, you can set the value of the tooltip and its duration. In the MouseLeave event handler, I am unsubscribing from the events for performance purpose. You can subscribe to these events using EventManager and its RegisterClassHandler().
public MainWindow()
{
    InitializeComponent();
    EventManager.RegisterClassHandler(typeof(GridViewCell), GridViewCell.MouseEnterEvent, new RoutedEventHandler(OnMouseEnterEvent));
    EventManager.RegisterClassHandler(typeof(GridViewCell), GridViewCell.MouseLeaveEvent, new RoutedEventHandler(OnMouseLeaveEvent));
}
 
private void OnMouseLeaveEvent(object sender, RoutedEventArgs e)
{
    var cell = sender as GridViewCell;
    cell.MouseEnter -= OnMouseEnterEvent;
    cell.MouseLeave -= OnMouseLeaveEvent;
}
 
private void OnMouseEnterEvent(object sender, RoutedEventArgs e)
{
    var cell = sender as GridViewCell;
    cell.ToolTip = cell.Value;
    ToolTipService.SetShowDuration(cell, 2000);
}

Hope this information is helpful.

Regards,
Dinko
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Jon
Top achievements
Rank 1
answered on 08 May 2018, 09:57 PM

That was exactly what I needed! 

In case this will be helpful to someone else - in my case I have 6 grids total but only this one is dynamically created, so I needed this code to fire only for that grid. To filter out only the grid I wanted (named dtg_PreviewMapGrid), I used this code: 

            if (!((GridViewCell)sender).ParentOfType<RadGridView>().Name.Equals(dtg_PreviewMapGrid.Name))
            {
                return;
            }

I am using dtg_PreviewMapGrid.Name instead of "dtg_PreviewMapGrid" as the quoted string would cause a breaking change if someone ever changes the name of the grid.I always try and avoid quoted strings in code.  

Much thanks - Jon 

1
Antti
Top achievements
Rank 1
Iron
answered on 15 Dec 2022, 06:59 AM | edited on 15 Dec 2022, 07:00 AM

Here is a little more straight forward solution:

public MyWindow() {
    InitializeComponent();
    DataContext = this;
    MyDataGridView.AutoGeneratingColumn += DataGridView_AutoGeneratingColumn;
}

private void DataGridView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e) {
    if (e.Column.Header.ToString() == "Status") {
        var baseStyle = (Style)e.Column.FindResource("GridViewHeaderCellStyle");
        var style = new Style() { TargetType = typeof(GridViewHeaderCell), BasedOn = baseStyle };
        style.Setters.Add(
            new Setter() {
                Property = ToolTipService.ToolTipProperty,
                Value = "My tooltip"
            });
        e.Column.HeaderCellStyle = style;
     }
 }
Tags
GridView
Asked by
Jon
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Jon
Top achievements
Rank 1
Antti
Top achievements
Rank 1
Iron
Share this question
or