DataGrid and ToolTip

R

René Nordby

I have a DataGrid with some TemplateColumns. One of these columns is a
CheckBox column, and the ChackBox have a ToolTip.

This application should run in several languages, why the ToolTip should be
changed dynamically.

I have solved this by changing the ToolTip in the ItemDataBound event, like
this


Private Sub myDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dtgKategori.ItemDataBound

If e.Item.ItemIndex <> -1 Then
CType(e.Item.FindControl("myCheckBox"), CheckBox).ToolTip = "My new ToolTip"
End If

End Sub


And everything works fine, but could it be done in another way.

What I'm thinking about is, that the CheckBox column is a multiple instance
of one CheckBox, so it feels a little stupid to change the ToolTip for each
Item in the DataGrid.

It should be possible to change the ToolTip before I make my databinding, so
I only should do it once, but...?

Please give me a hint...

René Nordby
 
A

Amar

Why don't you pass as a parameter the tooltip string you want to
display at ctor of the template column so every checkbox is created
has the correct tooltip.
 
R

René Nordby

Amar,

Thanks for your reply, but I'm not sure about what you mean, so could you
please give an code example.
 
A

Amar

I assume you create the template column in the code behind of GRID
like this...

TemplateColumn tc = new TemplateColumn();
tc.ItemTemplate = new GridItemCheckBoxTemplate("ToolTipText");
tc.HeaderStyle.Wrap = false;
tc.ItemStyle.Wrap = false;
Columns.Add(tc);

on the constructor of the template column....
this is a sample of my code so you will see some things that you want
need.

public class GridItemCheckBoxTemplate : System.Web.UI.ITemplate{

private string mToolTip;

public GridItemCheckBoxTemplate(string tooltip){
}

public void InstantiateIn(System.Web.UI.Control container){
CheckBox lc = new CheckBox();
// SOMEWHERE HERE YOU CAN ADD THE TOOLTIP BY USING THE STRING
PARAMETER
lc.ToolTip = mToolTip;
// SOMEWHERE HERE YOU CAN ADD THE TOOLTIP BY USING THE STRING
PARAMETER
lc.EnableViewState = true;
lc.DataBinding += new EventHandler(BindCol);
Literal startLC = new Literal();
startLC.Text = "<TABLE width=100% border=0 cellspacing=0
cellpading=0><TR><TD align=center>";
Literal endLC = new Literal();
endLC.Text = "</TD></TR></TABLE>";
container.Controls.Add(startLC);
container.Controls.Add(lc);
container.Controls.Add(endLC);
}
}
 
R

René Nordby

No, I'm not creating the TemplateColumn in the code behind. I have created
the column in the designer (Property Builder) in Visual Studio.Net. The only
things that change in the column, beside the data, is the HeaderText and the
ToolTip, so thats why I have created it on design time.

So what I was looking for was a way to change the ToolTip on the CheckBox
before the databinding, but I can't find a way to do that. It's seems that
there are no way to get grap of the CheckBox and it's properties before
databinding, or is there?
 
A

Amar

I don't know i have never used template column from designer, and i
don't have time to check it know, but i believe that there must be a
way to access the template columns...
Good luck.
 
R

Rick Spiewak

If your Checkbox is templated, you should be able to bind the tooltip in the
databindings collection in the designer for the column. The tooltip ideally
would be in a column in your datasource to make this work.
 
R

René Nordby

Thanks, I have tryied it, and it works.

But still, is there a way to get a reference to the templated CheckBox or
any other control in a TemplateColumn in a DataGrid, before databinding, ex.
in the Page_Load event?
 
R

Rick Spiewak

You should be able to use .FindControl in the page load event, based on the
item index of your choice.
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top