How DataGridView uses ToolTip controls to display cell contents in C # _c# tutorial

Source: Internet
Author: User

The example in this article describes how DataGridView uses the ToolTip control to display cell contents in C #. Share to everyone for your reference, specific as follows:

The code is as follows:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Data.SqlClient; namespace Exam2 {public partial class Mainform:form {private int cellcolumnindex = -1;//column index private int CE
      Llrowindex = -1;//row index public mainform () {InitializeComponent ();
      Set the correlation property value of the ToolTip tool this.dgvUserInfo.ShowCellToolTips = false;
      This.toolTip.AutomaticDelay = 0;
      This.toolTip.OwnerDraw = true;
      This.toolTip.ShowAlways = true;
      This.toolTip.ToolTipTitle = "";
      This.toolTip.UseAnimation = true;
    This.toolTip.UseFading = true; ///<summary>///Display user information///</summary>///<param name= "sender" ></param>//  /<param name= "E" ></param> private void Mainform_load (object sender, EventArgs e) {String sql = "Select User Id=userid, username =name, user logon name =uSername, user password =userpassword from userInfo ";
      SqlConnection conn = dbhelper.getconnection ();
      SqlDataAdapter adapter = new SqlDataAdapter (Sql,conn);
      DataSet ds = new DataSet (); Adapter.
      Fill (DS); This.dgvUserInfo.DataSource = ds.
    Tables[0]; } private void Dgvuserinfo_cellmouseleave (object sender, DataGridViewCellEventArgs e) {this.toolTip.Hide (th Is.dgvuserinfo),//After mouse move out of cell hide the hint tool} private void Dgvuserinfo_cellmouseenter (object sender, Datagridviewcelleventarg
      S e) {//Determine the validity of the selected cell if (E.rowindex < 0 | | E.columnindex < 0) {return;
      } this.toolTip.Hide (This.dgvuserinfo); This.cellcolumnindex = e.columnindex;//Get column index This.cellrowindex = e.rowindex;//Get row index if (This.cellcolumnindex &
        gt;= 0 && this.cellrowindex >= 0) {Point mousepos = PointToClient (mouseposition);//Get the current position of the mouse Gets the value in the cell where the mouse is moved string tip = This.dgvuserinfo[this.cellcolumnindex, This.cellrowindex].
        Value.tostring (); This.toolTip.Show (Tip, This.dgvuserinfo, mousepos);//Show prompt tool at specified location}//Draw hint tool private void Tooltip_draw (
      Object sender, Drawtooltipeventargs e) {e.graphics.fillrectangle (Brushes.aliceblue, e.bounds);
      E.graphics.drawrectangle (Pens.chocolate, New Rectangle (0, 0, e.bounds.width-1, e.bounds.height-1));
    E.graphics.drawstring (This.toolTip.ToolTipTitle + e.tooltiptext, E.font, brushes.red, e.bounds);

 }
  }
}

Read more about C # Interested readers can view the site topics: "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # object-oriented Program design Introductory Course" and "C # programming Thread Usage Skill Summary"

I hope this article will help you with C # programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.