title: Add tooltip to Items in a GridView after DataBind
body: <p>I've an ASP page that is populated entirely in code behind c#.</p>
  
  <p>I have to do this because I don't know how many GridView I get from my query.</p>
  
  <p>So I put a placeholder like this:</p>
  
  <pre><code>&lt;td&gt;
      &lt;asp:Label runat="server" ID="result"&gt;&lt;/asp:Label&gt;
      &lt;asp:HiddenField runat="server" ID="controlLoaded" /&gt;
      &lt;asp:PlaceHolder runat="server" ID="phTest"&gt;&lt;/asp:PlaceHolder&gt;
  &lt;/td&gt;
  </code></pre>
  
  <p>So I've created as many DataTables as result from my query. After that my code generates the same number of GridViews that I've to manage.</p>
  
  <p>My need is to "truncate" some data to max 25 letters and display a tooltip when passing over with the mouse.</p>
  
  <p>The code is:</p>
  
  <pre><code>gv.DataSource = dt;
          gv.DataBind();
  
          int maxWordLength = 25;
          foreach (GridViewRow row in gv.Rows)
          {
  
              for (int i = 3; i &lt;= 4; i++)
              {
                  string tmpValore = row.Cells[i].Text.ToString();
                  int lungValore = tmpValore.Length;
                  if (lungValore &gt; maxWordLength)
                  {
                      string nuovoValore = tmpValore.Substring(0, maxWordLength) + "...";
                      row.Cells[i].Text = nuovoValore;
                      row.Cells[i].ToolTip = tmpValore;
                  }
              }
          }
          gv.DataBind();
  </code></pre>
  
  <p>But the GridViews remain the same with long "strings".</p>
  
  <p>example: <code>"CENTRO DIURNO PSICHIATRICO CAMALEONTE"</code> have to show like:
  <code>"CENTRO DIURNO PSICHIATRI..."</code> with the whole string showed on mouse over</p>
  
  <p><strong>Screen Shot :</strong>
  <a href="https://i.stack.imgur.com/Xpvxp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Xpvxp.png" alt="This is a screenshot:"></a></p>
  
  <p>How can I reach such result? </p>
  
  <p>The entire code of the method is:</p>
  
  <pre><code> private GridView creaColonne()
  {
      GridView gv = new GridView();
  
      gv.AutoGenerateColumns = false;
  
      BoundField id = new BoundField();
      id.HeaderText = "Id";
      id.DataField = "Id";
      id.Visible = false;
      //id.HeaderStyle.Width = new Unit(45, UnitType.Pixel);
  
      BoundField dal = new BoundField();
      dal.HeaderText = "Dal";
      dal.DataField = "Dal";
      dal.HeaderStyle.Width = new Unit(65, UnitType.Pixel);
  
      BoundField al = new BoundField();
      al.HeaderText = "Al";
      al.DataField = "Al";
      al.HeaderStyle.Width = new Unit(35, UnitType.Pixel);
  
      BoundField unita = new BoundField();
      unita.HeaderText = "Unita";
      unita.DataField = "Unita";
      unita.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
      unita.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
      unita.HeaderStyle.Width = new Unit(245, UnitType.Pixel);
  
      BoundField mansione = new BoundField();
      mansione.HeaderText = "Mansione";
      mansione.DataField = "Mansione";
  
      mansione.HeaderStyle.Width = new Unit(245, UnitType.Pixel);
  
      BoundField ore = new BoundField();
      ore.HeaderText = "Ore";
      ore.DataField = "Ore";
      ore.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
      ore.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
      ore.HeaderStyle.Width = new Unit(50, UnitType.Pixel);
  
      BoundField perc = new BoundField();
      perc.HeaderText = "%";
      perc.DataField = "Perc";
      perc.HeaderStyle.Width = new Unit(50, UnitType.Pixel);
  
      gv.Columns.Add(id);
      gv.Columns.Add(dal);
      gv.Columns.Add(al);
      gv.Columns.Add(unita);
      gv.Columns.Add(mansione);
      gv.Columns.Add(ore);
      gv.Columns.Add(perc);
  
      return gv;
  }
  
  public object Popola(List&lt;DataTable&gt; ventilati) 
  {
      var ph = new PlaceHolder();
      int indice = 1;
      foreach (DataTable dt in ventilati)
      {
  
          Panel dynamicPanel = new Panel();
  
          string periodo = dt.Rows[0].Field&lt;string&gt;("Dal") + dt.Rows[0].Field&lt;string&gt;("Al");
          HtmlTable ht = new HtmlTable();
          HtmlTableRow htr = new HtmlTableRow();
          HtmlTableCell htc = new HtmlTableCell(); //Cella che contiene i dati
          HtmlTableCell htc2 = new HtmlTableCell(); //Cella che contiene i pulsanti
  
          htc2.Align = "center";
          ht.Width = "750px";
          ht.Border = 1;
          if (indice++ % 2 == 0)
          {
              ht.BgColor = "#66FFFF";
          }
          else
          {
              ht.BgColor = "#3399FF";
          }
  
          ht.CellPadding = 2;
  
          htc.Width = "720px";
          GridView gv = creaColonne();
  
  
          gv.DataSource = dt;
          gv.DataBind();
  
          int maxWordLength = 25;
          foreach (GridViewRow row in gv.Rows)
          {
  
              for (int i = 3; i &lt;= 4; i++)
              {
                  string tmpValore = row.Cells[i].Text.ToString();
                  int lungValore = tmpValore.Length;
                  if (lungValore &gt; maxWordLength)
                  {
                      string nuovoValore = tmpValore.Substring(0, maxWordLength) + "...";
                      row.Cells[i].Text = nuovoValore;
                      row.Cells[i].ToolTip = tmpValore;
                  }
              }
          }
          gv.DataBind();
          gv.CssClass = "tab_gen";
          gv.Width = Unit.Percentage(100);
  
          gv.ID = "gvEdit" + periodo.ToString();
  
          htc.Controls.Add(gv);
  
          dynamicPanel.ID = "pnlEdit" + periodo.ToString();
          int finePeriodo = int.Parse(dt.Rows[0][2].ToString());
          int inizioPeriodo = int.Parse(dt.Rows[0][1].ToString());
  
          DateTime today = DateTime.Today;
          int periodoOggi = today.Year * 100 + today.Month;
  
          bool editable = false;
          if (today.Day &lt; 16 || finePeriodo &gt;= periodoOggi)
          {
              editable = true;
          }
  
          htc2.Width = "30px";
          if (editable)
          {
              editaButton = new ImageButton();
              editaButton.Command += new CommandEventHandler(this.editaButton_Click);
              //editaButton.OnClientClick += new EventHandler(this.editaButton_Click);
              editaButton.ID = "btnEdit" + periodo.ToString();
              editaButton.ImageUrl = "~/image/edit.png";
              editaButton.ToolTip = "Edita";
  
              htc2.Controls.Add(editaButton);
              Label newLine = new Label();
              newLine.Text = "&lt;br /&gt;";
              htc2.Controls.Add(newLine);
  
          }
  
          duplicaButton = new ImageButton();
          duplicaButton.Command += new CommandEventHandler(this.duplicaButton_Click);
          //duplicaButton.OnClientClick += new EventHandler(this.duplicaButton_Click);
          duplicaButton.ID = "btnDuplica" + periodo.ToString();
          duplicaButton.ImageUrl = "~/image/duplicate.png";
          duplicaButton.ToolTip = "Duplica";
  
  
          htc2.Controls.Add(duplicaButton);
  
          htr.Controls.Add(htc);
          htr.Controls.Add(htc2);
          ht.Controls.Add(htr);
          dynamicPanel.Controls.Add(ht);
  
          ph.Controls.Add(dynamicPanel);
  
      }
  
      ph.DataBind();
  
      return ph;
  }
  </code></pre>