Method for datagridview in C to display cell contents using tooltip control
- 2021-10-13 08:26:09
- OfStack
This article illustrates how datagridview in C # uses tooltip controls to display the contents of cells. Share it for your reference, 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 cellRowIndex = -1;// Row index
public MainForm()
{
InitializeComponent();
// Set the related property values 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 Users ID=userID, User name =name, User login 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(this.dgvUserInfo);// Hide prompt tool after mouse moves out of 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;// Get Column Index
this.cellRowIndex = e.RowIndex;// Get Row Index
if (this.cellColumnIndex >= 0 && this.cellRowIndex >= 0)
{
Point mousePos = PointToClient(MousePosition);// Gets the current position of the mouse
// Gets the value in the cell where the mouse moves
string tip = this.dgvUserInfo[this.cellColumnIndex, this.cellRowIndex].Value.ToString();
this.toolTip.Show(tip, this.dgvUserInfo, mousePos);// Displays the prompt tool at the specified location
}
}
// Drawing prompt 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);
}
}
}
For more readers interested in C # related content, please check out the topics on this site: "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"
I hope this article is helpful to everyone's C # programming.