C# datagridview toolTip悬浮框显示详细内容

如图所示,这是公司要我实现的需求。

我的本次采购数量是多个合同的和,所以我想在看这一条的时候显示他们分别是哪个合同的,分别采购了多少。

第一拖一个tooptip控件实现Draw事件

给我们的控件设置下属性

            this.dgv_goodsSum.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;

(不能漏哦!)

第二实现datagridview的两个事件CellMouseEnter(鼠标进入单元格时触发)和CellMouseLeave(鼠标离开单元格时触发)

然后就是主要代码:如下

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

        private void dgv_goodsSum_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            //判断选择单元格是否有效
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }

            this.toolTip1.Hide(this.dgv_goodsSum);
            int i = e.ColumnIndex;//获取鼠标停留的列索引
            int ei = e.RowIndex;//获取停留的行索引

            if (i >= 0 && ei >= 0)
            {
                Point mousePos = PointToClient(MousePosition);//获取鼠标当前的位置
                //获取鼠标停留的单元格中的值
                string tip = this.dgv_goodsSum[3, ei].Value.ToString();//获取合同号 这里我写的是死的,因为我只要它这一个值,当然大家如果想要获取到每一列的值的话就把我的3改成i就够了
                string tip1 = this.dgv_goodsSum[4, ei].Value.ToString();//获取物品号
                string tips = "------------------------\n  物品编号:" + tip1 + "  \n-------------------------\n";
                string[] strs;

                strs = tip.Split(';');
                for (int j = 0; j < strs.Length; j++)
                {
                    string sql = "select b.contractNo,a.thispurchaseqty, a.inqty from purchase_contract b inner join purchase_contract_goods a on a.mainid = " + strs[j] + " and b.id = " + strs[j] + " and goodsNo = '" + tip1 + "'";
                    using (DataAdapter da = new DataAdapter())
                    {
                        DataTable dt = new DataTable();
                        da.Text = sql;
                        da.Fill(dt);
                        tips += "   合同号:" + dt.Rows[0][0].ToString() + "\n采购数量:" + dt.Rows[0][1].ToString() + "\n实际入库:" + dt.Rows[0][2].ToString() + "\n-------------------------\n";
                    }
                }

                this.toolTip1.Show(tips, this.dgv_goodsSum, new Point(mousePos.X - 700, ei * 20));//在指定位置显示提示框
            }
        }

        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);
        }

 

好吧!就这些

本人初学者,希望能给大家帮助,也希望大家能有更好的代码例子让我也学习学习。

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值