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.