The datagridview uses the tooltip control to display the cell content.

Source: Internet
Author: User

CodeAs 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 cellrowindex =-1; // row Index
Public mainform ()
{
Initializecomponent ();

// Set the property value of the prompt 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, user name = Name, user 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)
{< br> This. tooltip. hide (this. dgvuserinfo); // hide the prompt tool after removing the mouse from the cell
}

Private void dgvuserinfo_cellmouseenter (Object sender, datagridviewcelleventargs 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; // obtain the column Index
This. cellrowindex = E. rowindex; // obtain the row index.

If (this. cellcolumnindex> = 0 & this. cellrowindex> = 0)
{
Point mousepos = pointtoclient (mouseposition); // obtain the current mouse position
// Obtain the value of the cell to which the mouse is moved
String tip = This. dgvuserinfo [This. cellcolumnindex, this. cellrowindex]. value. tostring ();
This. tooltip. Show (TIP, this. dgvuserinfo, mousepos); // display the prompt tool at the specified position
}
}

// drawing prompt tool
private void tooltip_draw (Object sender, drawtooltipeventargs e)
{< br> 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);
}< BR >}

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.