Displaying a tooltip in a datagrid cell - can it be done?

G

Guest

I'm trying to work with a datagrid column in order to display a tooltip in a
datagrid cell. The reason I am doing this is because I have some long strings
being returned and I don't want the rows of the datagrid expaning down to
much. I basically want to display the first 50 characters in the grid cell
and then display the rest as a tooltip in the grid cell. I'm not sure if this
is possible but if anyone can help me do this I would be very grateful. I
have started this and am able to do the first part and get only the first 50
characters to be displayed back. I can also get the rest of the string set to
a new variable but I don't know how to use this properly and display a
tooltip with the string value for only that cell. Hope everyone knows what
i'm trying to do and that i've explained myself well enough. I've included
the code to show what I've down and also to give an idea of what i'm trying
to do. Any help at all would be very much appreciated.

HTML for the Column
<asp:TemplateColumn Visible="True" HeaderText="Start Date">
<ItemTemplate>
<%#getCompanyName(DataBinder.Eval(Container.DataItem,"CompanyName").ToString())%>
</ItemTemplate>
</asp:TemplateColumn>

Code Behind with comments of what I need to do

public string getCompanyName(string p_sFieldValue )
{
int iLength = p_sFieldValue.Length;
int iLengthToUse = 0;
int iRemainder = 0;

if (p_sFieldValue == DBNull.Value.ToString())
{
return string.Empty;
}
else if(iLength > 50)
{
//In here I basically would like to only return the first
//50 characters of the string and then for the rest of the
//string I’d like the details to be displayed in a tooltip
//for that cell. I know the below code won’t work because
//of the e but its just to give an idea of what I need to do.
//If someone knows how I can display a tooltip for the rest
//of the string I’d be really really grateful
//I’m not sure if this can be done but if anyone has any
//ideas I’d really appreciate it. Thanks
/*
iRemainder = p_sFieldValue.Length - 50;
string sTooltipText = p_sFieldValue.Substring(50,iRemainder);
if(e.Item.ItemType == ListItemType. SelectedItem)
{
e.Item.Cells[1].ToolTip = sTooltipText;
}
*/
//This bit works fine
string sCompanyName = p_sFieldValue.Substring(0,50); return
sCompanyName;
}
else
{
return p_sFieldValue;
}
}
 
M

Marcie Jones

Stephen,
Your idea will work fine, just move this logic to the ItemDataBound
event of the grid, there you can use "e" as well as set the text of
your cell.

Marcie
 

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

Top