Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help on how to show toolTip of content from datagridview cell. the built in ShowCellContent property which is set to true by default is not working well, it doesnt show for every cell the content, so is there some code which I can use to make this work

thanks
Posted

RowDataBound event of GridView

C#
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            if (e.Row.Cells[i].Controls.Count == 0)
            {
                e.Row.Cells[i].ToolTip = e.Row.Cells[i].Text;
            }
            else
            {
                e.Row.Cells[i].ToolTip = GetObjectType(e.Row.Cells[i].Controls[1]);
            }
        }
    }
}

private String GetObjectType(Control ctrl)
{
    switch (ctrl.GetType().Name)
    {
        case "Label":
            return ((Label)ctrl).Text;
        case "TextBox":
            return ((TextBox)ctrl).Text;
    }
    return "";
}


Regards,
Senthil
 
Share this answer
 
v3
Comments
Praveen Meghwal 11-Aug-11 7:58am    
This is nice approach but in case of TemplateField it will not work. It gives text property as empty string value.
senthil sennu 11-Aug-11 8:29am    
Yes, You are correct. I update the code. please ref. I have added one new method GetObjectType which will check of control type. You can add your control which is used inside your grid. Normally 2nd control will be the template control (e.Row.Cells[i].Controls[1])). Pls chk. If i am wrong pls correct me.
Praveen Meghwal 11-Aug-11 8:55am    
Yes, You are correct. This is working fine :)
There is a property ShowCellToolTips [^] in DataGridView control.
Try this.
 
Share this answer
 
Comments
shonezi 11-Aug-11 7:03am    
it's not working properly, it doesnt show contents of each cell that I hover over
In RowDataBound event of GridView for Datarow write

C#
e.Row.Cells[0].ToolTip = "Your ToolTip Text";


Regards
Praveen
 
Share this answer
 
Comments
shonezi 11-Aug-11 7:03am    
I dont want to type in text of tooltip, I want that tooltip shows text which is in the cell
Praveen Meghwal 11-Aug-11 7:52am    
You can get DataItem of the current row as:

DataRowView drv = (DataRowView)e.Row.DataItem;

Here i have used DataRowView because I am binding the grid with DataTable. Above line will change according to the Datasource you are setting for the grid.

After getting the DataItem you can use following lines:

e.Row.Cells[0].ToolTip = drv[0].ToString();
e.Row.Cells[1].ToolTip = drv[1].ToString();

Regards
Praveen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900