views:

364

answers:

3

Hello all...complete novice at work (who is also ill and feeling particularly thick)

I have the following code that gives me a generic "tool tip text" for each heading in a gridview....great.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.Header) 
    { 
        foreach (TableCell cell in e.Row.Cells) 
        { 
            foreach (System.Web.UI.Control ctl in cell.Controls) 
            { 
                if (ctl.GetType().ToString().Contains("DataControlLinkButton")) 
                { 
                    cell.Attributes.Add(
                        "title", "tooltip text for " + ((LinkButton)ctl).Text);
                }
            } 
        }
    }
}

What is not so great is that I obviously don't want all of the cells to return the same generic ' tooltip text for'.

How would a simpleton like me adapt the code so that for the ProductID heading cell the tt says "a unique product reference", for a Product Description heading cell the tt returns "description of the the product".

Apologies for the dim question.

A: 

I would use a TemplateField:

<asp:GridView ...>
   ...
   <asp:TemplateField>
      <ItemTemplate>
         <a href='<%# Eval("ProductID", "Edit.aspx?{0}") %>' title='<%# Eval("ProductDescription") %>'><%# Eval("ProductID") %></a>
      </ItemTemplate>
   </asp:TemplateField>
   ...
</asp:GridView>

More about using Templates

jrummell
A: 

If I understand correctly, you want the tooltip to be set corresponding to the column title ?

You could do something like this :

if (ctl.GetType().ToString().Contains("DataControlLinkButton")) 
{
    String headerText = cell.Text;
    cell.Attributes.Add("title", headerTooltips[headerText]);
}

where headerTooltips is a dictionary defined before :

Dictionary<String, String> headerTooltips = new Dictionary<String, String>();
headerTooltips["Product ID"] = "A unique product ID";
[...]
Wookai
Hello Wookai..I have tried this but I get a "The given key was not present in the dictionary." on the 'cell.attributes.add('title'...' line...any ideas why?
MrDean
Maybe you did not create the dictionary with the right keys ? How do you construct it ? Try to print headerText et see if it corresponds to what you used to create the dictionary.
Wookai