您的位置:首页 > 编程语言 > C#

C#中datagridview使用tooltip控件显示单元格内容与datagridview自带的tooltip显示单元格内容的方法

2016-11-05 02:25 916 查看

1、datagridview自带的tooltip显示单元格内容的方法

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.dataGridView1.ShowCellToolTips = true;
this.toolTip1.AutomaticDelay = 0;//提示延迟
this.toolTip1.ShowAlways = true;//是否显示文本
this.toolTip1.ToolTipTitle = "";//窗口标题
this.toolTip1.UseAnimation = true;//动画效果
this.toolTip1.UseFading = true;//淡入淡出效果
}

private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("server = XQ-20160210KQLE\\SA;uid = sa;pwd = 123456;database = JYXinXi");
try
{
con.Open();
string strselect = "select 仓库名称,负责人,联系电话,仓库地址,备注 from CanKuBiao";
SqlDataAdapter da = new SqlDataAdapter(strselect, con);
da.Fill(dt);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
finally
{
con.Close();
this.dataGridView1.DataSource = dt;
}
}

private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || e.ColumnIndex < 0)
{
return;
}
else
{
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = "qqq";
}
}

private void toolTip1_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.toolTip1.ToolTipTitle + e.ToolTipText, e.Font, Brushes.Red, e.Bounds);
}

private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{////鼠标移出单元格后隐藏提示工具
this.toolTip1.Hide(this.dataGridView1);
}
}
}


2、datagridview使用tooltip控件显示单元格内容

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.dataGridView1.ShowCellToolTips = false;
this.toolTip1.AutomaticDelay = 0;//提示延迟
this.toolTip1.OwnerDraw = true;//自己绘制或者系统绘制
this.toolTip1.ShowAlways = true;//是否显示文本
this.toolTip1.ToolTipTitle = "";//窗口标题
this.toolTip1.UseAnimation = true;//动画效果
this.toolTip1.UseFading = true;//淡入淡出效果
}

private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("server = XQ-20160210KQLE\\SA;uid = sa;pwd = 123456;database = JYXinXi");

try
{
con.Open();
string strselect = "select 仓库名称,负责人,联系电话,仓库地址,备注 from CanKuBiao";
SqlDataAdapter da = new SqlDataAdapter(strselect, con);
da.Fill(dt);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
finally
{
con.Close();
this.dataGridView1.DataSource = dt;
}
}

private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || e.ColumnIndex < 0)
{
return;
}
else
{
this.toolTip1.Hide(this.dataGridView1);
this.toolTip1.Show("qqqq", this.dataGridView1, Cursor.Position);
}

}

private void toolTip1_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.toolTip1.ToolTipTitle + e.ToolTipText, e.Font, Brushes.Red, e.Bounds);
}

private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{////鼠标移出单元格后隐藏提示工具
this.toolTip1.Hide(this.dataGridView1);
}
}


问题1:
b882
使用tooltip控件显示程序中提示文本显示的位置不在datagridview表中。


提示文本自定义绘制可显示,只是显示的位置不在表中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐