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

How to display column description as a tooltip

14 Answers 250 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve Newbery
Top achievements
Rank 1
Steve Newbery asked on 21 Apr 2008, 11:33 AM
I'd like to have the SQL Server column description property as a tooltip in my radGrid. I can easily enough get the descriptions by querying sysproperties (or sys.columns in SQL Server 2005), but I'm not sure about the next step.

I'm guessing I would populate a dataset with the descriptions in the form's Load event, and then in maybe the ItemCreated event of each column I would set the tooltip.

If anyone has an example of doing this, or something similar, I'd be eternally grateful to see it!

One thing, I don't see a tooltip property in radGrid per column - do I have to convert each column to a template and use the title property?

14 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 22 Apr 2008, 10:46 AM
Hi Steve,

You can set the text for each cell in a given column, you can use code like the following:

.cs
  protected void RadGrid1_PreRender(object sender, EventArgs e)  
    {  
        foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)  
        {  
            dataItem["CategoryID"].ToolTip = dataItem["CategoryID"].Text;  
        }  
    } 

Where "CategoryID" is the unique name of a column.
I hope this information helps.

All the best,
Yavor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Steve Newbery
Top achievements
Rank 1
answered on 24 Apr 2008, 08:59 PM
Thanks for the reply Yavor.

This solution would be for cells of the data rows in the rad Grid, is that correct?

Is it possible to do this only for the headings of each column? I fear that tooltips in each cell might get annoying for the users.
0
Shinu
Top achievements
Rank 2
answered on 25 Apr 2008, 04:02 AM
Hi Steve,

I hope the following help article will help you.
Adding tooltips for grid items

Thanks
Shinu.
0
Steve Newbery
Top achievements
Rank 1
answered on 25 Apr 2008, 07:32 AM
Shinu that's it - yes!

Many thanks.
0
Steve Newbery
Top achievements
Rank 1
answered on 28 Apr 2008, 08:21 AM
One thing I can't get to work, is to check that a heading link exists, using FindControl.

I'm setting the tooltips by reading the SQL Server description property for each column. This works fine with the code below:

        ' Set tooltips for the header cells  
        If (TypeOf e.Item Is GridHeaderItem) Then  
            Dim headerItem As GridHeaderItem = CType(e.Item, GridHeaderItem)  
 
            ' Get the column descriptions  
            Dim Reader As SqlDataReader = SqlDataSource2.Select(DataSourceSelectArguments.Empty)  
            While Reader.Read()  
 
                If Not (Reader("Description") Is DBNull.Value) Then  
                    CType(headerItem(Reader("ColumnName")).Controls(0), LinkButton).ToolTip = Reader("Description")  
                End If  
 
            End While  
 
        End If  
 

But, I want to be sure the column exists in the grid. So I adapt the code to do a FindControl before setting the tooltip:

        ' Set tooltips for the header cells  
        If (TypeOf e.Item Is GridHeaderItem) Then  
            Dim headerItem As GridHeaderItem = CType(e.Item, GridHeaderItem)  
 
            ' Get the column descriptions  
            Dim btn As LinkButton  
            Dim Reader As SqlDataReader = SqlDataSource2.Select(DataSourceSelectArguments.Empty)  
            While Reader.Read()  
 
                btn = DirectCast(headerItem.FindControl(Reader("ColumnName")), LinkButton)  
 
                ' This doesn't work for some reason  
                If Not (btn Is Nothing) Then  
                    If Not (Reader("Description") Is DBNull.Value) Then  
                        CType(headerItem(Reader("ColumnName")).Controls(0), LinkButton).ToolTip = Reader("Description")  
                    End If  
                End If  
 
            End While  
 
        End If  
 

But the problem is that btn is always nothing, in other words the FindControl never finds the heading link.

Can you help please?
0
Shinu
Top achievements
Rank 2
answered on 28 Apr 2008, 12:24 PM
Hi,

You need to find the LinkButton in the grid using the Controls collection and not FindControl method

if

(e.Item is GridHeaderItem)

{

GridHeaderItem headerItem=(GridHeaderItem)e.Item;

LinkButton btn = (LinkButton)headerItem["ColumnUniqueName"].Controls[0];

}

Thanks,
Shinu

0
Steve Newbery
Top achievements
Rank 1
answered on 28 Apr 2008, 02:00 PM
Shinu, thanks for the reply, but I think you misunderstood the question. I can already refer to the heading LinkButton ok and set the tooltip - see the first section of my code.

But my problem is how to check that the LinkButton actually exists, before attempting to set the tooltop. My second section of code tries unsuccessfully to do this.
0
Steve Newbery
Top achievements
Rank 1
answered on 29 Apr 2008, 07:52 AM
Can someone help me here? I'd appreciate it.
0
Yavor
Telerik team
answered on 29 Apr 2008, 08:02 AM
Hi Steve,

Once you get a reference to the header item, check if 

header.Controls.Count

is more than 0. If that is the case, you can iterate through the controls, and check which one is the LinkButton.

This will give you a safe approach to referencing the control, and setting the tooltip.

I hope this information helps.

Sincerely yours,

Yavor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Steve Newbery
Top achievements
Rank 1
answered on 29 Apr 2008, 11:56 AM
Yavor, thanks. So now my code is like below - in fact an adaptation of the help file code, and it works perfectly:

    Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs)  
   
        ' Set tooltips for the header cells  
        If (TypeOf e.Item Is GridHeaderItem) Then  
            Dim headerItem As GridHeaderItem = CType(e.Item, GridHeaderItem)  
 
            ' Get the column descriptions from SQL Server  
            Dim Reader As SqlDataReader = SqlDataSource2.Select(DataSourceSelectArguments.Empty)  
            While Reader.Read()  
 
                'look for the column - if we find it we'll set the tooltip  
                For Each column As GridColumn In RadGrid1.MasterTableView.RenderColumns  
                    If (TypeOf column Is GridBoundColumn) Then  
                          
                        'if this is the column we want, and there is some tooltip text  
                        If column.UniqueName = Reader("ColumnName") And (Not (Reader("Description") Is DBNull.Value)) Then  
                              
                            'if the sorting feature of the grid is enabled  
                            CType(headerItem(column.UniqueName).Controls(0), LinkButton).ToolTip = Reader("Description")  
 
                            'if the sorting feature is disabled for this column or the entire grid  
                            headerItem(column.UniqueName).ToolTip = Reader("Description")  
                                  
                        End If  
                    End If  
                Next  
            End While  
        End If  
 

This means that we can set the Description property for each column in SQL Server, and that description is immediately displayed as a tooltip in the grid.

Thanks for your help!
0
Yavor
Telerik team
answered on 29 Apr 2008, 12:11 PM
Hello Steve,

I hope the application is functioning as per your requirements now.
You can also consider moving this code in the ItemDataBound event handler, or get a reference to the header in the PreRender event handler.

Best wishes,
Yavor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Steve Newbery
Top achievements
Rank 1
answered on 30 Apr 2008, 08:32 AM
Yes it's working very well now, thanks.

Would there be any particular reason or advantage to using the ItemDataBound  or the PreRender events?
0
Yavor
Telerik team
answered on 30 Apr 2008, 11:43 AM
Hi Steve,

It depends on which stage you want to handle the logic. Also, the ItemDataBound is called for each item, whereas PreRender is called only once per lifecycle. in the ItemCreated no data may be available for the items (the data is fetched later on when the itemDataBound event handler is called. The ItemDataBound event handler is not called on each page cycle, since the items may not be bound each time, whereas PreRender is called on each page cycle. Depending on your implementation, it may be appropriate to put the code in different handlers - i added the information merely for the sake of completeness.

Sincerely yours,
Yavor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Steve Newbery
Top achievements
Rank 1
answered on 02 May 2008, 02:07 PM
Yavor - many thanks for the interesting and useful answer.
Tags
Grid
Asked by
Steve Newbery
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Steve Newbery
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or