Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return a tooltip for a datagrid cell. Is it possible 2

Status
Not open for further replies.

lsmyth1717

Programmer
Mar 25, 2005
44
GB
I have the following column and so far im able to bind the column to
CompanyName and only return the first 50 characters. The second function i'd
like to retun a tooltip for that datagrid cell but i'm not sure if this is
possible and if so I don't know how to right a function to return a tooltip.
Can someone please help me with this.

<asp:TemplateColumn Visible="True" HeaderText="Start Date">
<ItemTemplate>
<span
title="<%#getCompanyName(DataBinder.Eval(Container.DataItem,"CompanyName").ToString())%>"><%#partial_text_tooltip%></span>
</ItemTemplate>
</asp:TemplateColumn>
 
If it's a datagrid you are using then you can set a ToolTip on the ItemCreated event of the DataGrid by using somehting like:
Code:
e.Item.Cells(0).ToolTip = "Test"

----------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Yeah cheers i've done that before. Unforuntately this won't work in my case as i'm trying to dynamically create a tooltip for a specific cell in the datagrid when there are more than 50 characters because thats all I'm displaying.

Basically I need to write a function (partial_text_tooltip) that returns a tooltip containing the full value of the bound field - CompanyName. Have you any ideas?

 
It won't work or at least I don't see how it could work because in the item created how am I going to set the tooltip for the value of an individual cell in the datagrid. Also I only want the tooltip to be created if the value of the bound field is more than 50.

Sorry I'm probably not explaining myself very well. Hope you know what I mean.
 
OK maybe I didn't explain myself clearly either. To demonstrate what I mean I've done a really simple example.

First add a DataGrid named DataGrid1 to a new form and add the following code:
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        ' Declarations
        Dim dt As New DataTable
        Dim dr As DataRow

        ' Create a datatable with ten sample records
        dt.Columns.Add("Item1")
        For i As Integer = 1 To 10
            dr = dt.NewRow
            dr(0) = i
            dt.Rows.Add(dr)
        Next

        ' Bind the DataGrid to the DataTable
        DataGrid1.DataSource = dt
        DataGrid1.DataBind()
    End Sub

    Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
        ' Check if the item is a header row
        If e.Item.ItemType = ListItemType.Header Then
            Dim strShortText As String = ""
            ' Check if the length of the text is greater than 1
            If e.Item.Cells(0).Text.Length > 1 Then
                ' If it is greater set the tooltip to be the full text
                e.Item.Cells(0).ToolTip = e.Item.Cells(0).Text
                ' Set the actual value that is displayed to just be the first letter
                strShortText = e.Item.Cells(0).Text.Substring(0, 1)
                e.Item.Cells(0).Text = strShortText
            End If
        End If
    End Sub
When you load the page you'll notice that the column header only displays the letter "I" but the tooltip for the column says "Item1".

Hopefully you understand what I was trying to get and and if it is suitable you should be able to adapt my code for your needs.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Yeah I'll have a go at working with this. Cheers for your help.
 
Sorry got this working but still have a problem trying to check against the actual cell rows and not the header. Tried this: -
e.Item.ItemType = ListItemType.Item
as oppose to checking the header but keeps coming up in the quick watch that its an empty string. Any ideas
 
AIf you are checking the actual rows and not the header you will have to do it in the ItemDataBound event. Something like this should work:
Code:
    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        ' Check if the item is an item row
        Select Case e.Item.ItemType

            Case ListItemType.AlternatingItem, ListItemType.Item
                Dim strShortText As String = ""
                ' Check if the length of the text is greater than 1
                If e.Item.Cells(0).Text.Length > 1 Then
                    ' If it is greater set the tooltip to be the full text
                    e.Item.Cells(0).ToolTip = e.Item.Cells(0).Text
                    ' Set the actual value that is displayed to just be the first letter
                    strShortText = e.Item.Cells(0).Text.Substring(0, 1)
                    e.Item.Cells(0).Text = strShortText
                End If

        End Select

    End Sub

----------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Worked perfectly and is by far the best way to do this. Never really used the ItemDataBound event but i can see how useful it could be now. Cheers.
 
I cannot tell you how cool this is. So i give ya a star for an extremely helpful post.

Thanks,
IBACFII
 
Glad I could help!

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top