Adding Tooltip text to a cell in a datagrid

C

Craig G

what way do you code it? i tried the following but it wouldnt display it

lblAdd_info is a hidden field in the cell itself that contains the data i
want to display in the tooltip text. i added the following into the
ItemDatabound event for the grif but it wont display


If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType =
ListItemType.Item Then

e.Item.Cells(5).ToolTip = "Craigs ToolTip Test!!"



End If



ideally i wanna set the tooltip text to a value returned from a dataset for
each row. but i cant even get it to display just a test string!

Cheers,
Craig
 
K

Ken Cox [Microsoft MVP]

Hi Craig,

Try it on the ItemCreated event? Here's some code, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemCreated _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemCreated
If e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item Then
e.Item.Cells(0).ToolTip = "Tooltip for item: " &
e.Item.ItemIndex
e.Item.Cells(1).ToolTip = "Tooltip for item: " &
e.Item.ItemIndex
e.Item.Cells(2).ToolTip = "Tooltip for item: " &
e.Item.ItemIndex
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 1
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
 
K

Ken Cox [Microsoft MVP]

Oops. I wasn't paying enough attention. Here's what should work... get the
label from the hidden cell...

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item Then
e.Item.Cells(0).ToolTip = e.Item.Cells(3).Text
e.Item.Cells(1).ToolTip = "Test" & _
CType(e.Item.FindControl("secondstring"), Label).Text
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("SecondString", GetType(String)))
Dim i As Integer
For i = 0 To 1
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = "Second string " + i.ToString()
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource


<asp:datagrid id="DataGrid1" runat="server"
autogeneratecolumns="False">
<columns>
<asp:templatecolumn>
<itemtemplate>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.IntegerValue") %>'>
</asp:label>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn>
<itemtemplate>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Stringvalue") %>'>
</asp:label>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn>
<itemtemplate>
<asp:Label runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.CurrencyValue") %>'>
</asp:label>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn>
<itemtemplate>
<asp:Label Visible="False" ID="secondstring"
runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.SecondString")
%>'>
</asp:label>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
 
C

Craig G

that worked a treat Ken

Cheers!

Ken Cox said:
Hi Craig,

Try it on the ItemCreated event? Here's some code, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemCreated _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemCreated
If e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item Then
e.Item.Cells(0).ToolTip = "Tooltip for item: " &
e.Item.ItemIndex
e.Item.Cells(1).ToolTip = "Tooltip for item: " &
e.Item.ItemIndex
e.Item.Cells(2).ToolTip = "Tooltip for item: " &
e.Item.ItemIndex
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 1
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Craig G said:
what way do you code it? i tried the following but it wouldnt display it

lblAdd_info is a hidden field in the cell itself that contains the data i
want to display in the tooltip text. i added the following into the
ItemDatabound event for the grif but it wont display


If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType =
ListItemType.Item Then

e.Item.Cells(5).ToolTip = "Craigs ToolTip Test!!"



End If



ideally i wanna set the tooltip text to a value returned from a dataset
for
each row. but i cant even get it to display just a test string!

Cheers,
Craig
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top