DataGridView控件添加鼠标选定ToolTip显示合计

27 篇文章 0 订阅
18 篇文章 0 订阅
/// <summary>
        /// DataGridView多行选择求和
        /// </summary>
        /// <param name="myDataGridView">要求合计提示的DataGridView控件</param>
        /// <param name="TooLar">传入的ToolTip控件.</param>
        /// <returns></returns>
        public bool DataGridViewSum(DataGridView myDataGridView, ToolTip TooLar)
        {
            if (myDataGridView.SelectedCells.Count < 2)
            {
                TooLar.RemoveAll();
                return false;
            }
            //如果选择的单元格是全部,且大于:10000,则认为是不是求合计的操作
            if (myDataGridView.SelectedCells.Count > 10000 && myDataGridView.SelectedCells.Count == myDataGridView.GetCellCount(DataGridViewElementStates.None))
            {
                TooLar.RemoveAll();
                return false;
            }
 
            DataSet DS = new DataSet();
            DataTable DtGroup = new DataTable("DtGroup");//保存列名
            System.Collections.ArrayList NameMaxLength = new System.Collections.ArrayList();//保存DT中列名的长度
            System.Collections.ArrayList ValueMaxLength = new System.Collections.ArrayList();//保存DT中列名的长度
            DataTable DT = new DataTable("DT");//保存列名及数据
            DtGroup.Columns.Add("ColumnName", System.Type.GetType("System.String"));//字段名
 
            DT.Columns.Add("ColumnName", System.Type.GetType("System.String"));//字段名
            DT.Columns.Add("ColumnValue", System.Type.GetType("System.Decimal"));//字段值
 
            foreach (DataGridViewCell DC in myDataGridView.SelectedCells)
            {
                if (DC.ValueType.Name.ToLower() == "decimal" || DC.ValueType.Name.ToLower() == "int32")
                {
                    DataRow DR;
                    DR = DT.NewRow();
                    DR["ColumnName"] = myDataGridView.Columns[DC.ColumnIndex].Name;
                    if (DC.Value == null)
                    {
                        DR["ColumnValue"] = 0;
                    }
                    else
                    {
                        DR["ColumnValue"] = DC.Value;
                        ValueMaxLength.Add(DC.Value.ToString().Length);
                    }
                    DT.Rows.Add(DR);
                    //在现有DtColName中查找是否包含了这个列名
                    if (DtGroup.Select("ColumnName='" + myDataGridView.Columns[DC.ColumnIndex].Name + "'").Length == 0)
                    {
                        DataRow DR1;
                        DR1 = DtGroup.NewRow();
                        DR1["ColumnName"] = myDataGridView.Columns[DC.ColumnIndex].Name;
                        DtGroup.Rows.Add(DR1);
                        NameMaxLength.Add(myDataGridView.Columns[DC.ColumnIndex].Name.Length);
                    }
                }
            }
            DS.Tables.Add(DT);
            DS.Tables.Add(DtGroup);
 
            DataRelation dRelation = new DataRelation("dRelation", DS.Tables["DtGroup"].Columns["ColumnName"], DS.Tables["DT"].Columns["ColumnName"]);
            DS.Relations.Add(dRelation);
            DtGroup.Columns.Add("ColumnSum").Expression = "sum(child(dRelation).ColumnValue)";
            string decSum = DT.Compute("sum(ColumnValue)", "").ToString();
 
            NameMaxLength.Add("总合计:".Length);
            ValueMaxLength.Add(decSum.Length);
            DtGroup.AcceptChanges();
            string Tall = "";
            NameMaxLength.Sort();
            ValueMaxLength.Sort();
            int NameMaxLen = 0;
            NameMaxLen = int.Parse(NameMaxLength[NameMaxLength.Count - 1].ToString());
            int ValueMaxLen = 0;
            ValueMaxLen = int.Parse(ValueMaxLength[ValueMaxLength.Count - 1].ToString());
            for (int i = 0; i < DtGroup.Rows.Count; i++)
            {
                string strOneLine = "";
                strOneLine = (DtGroup.Rows[i]["ColumnName"].ToString() + ":").PadRight(NameMaxLen + 1, ' ');
                strOneLine = strOneLine + DtGroup.Rows[i]["ColumnSum"].ToString().PadLeft(ValueMaxLen, ' ');
                Tall = Tall + strOneLine + System.Environment.NewLine;
            }
            Tall = Tall + "总合计:".PadRight(NameMaxLen + 1, ' ') + decSum.PadLeft(ValueMaxLen, ' ');
 
            myDataGridView.ShowCellToolTips = false;
            TooLar.ToolTipIcon = ToolTipIcon.Info;
            TooLar.IsBalloon = true;//汽球形状
            TooLar.ToolTipTitle = "鼠标选定合计信息:";
            TooLar.UseAnimation = true;//使用动画效果
            TooLar.UseFading = true;//淡入淡出效果
            TooLar.BackColor = Color.Wheat;//工具提示的背景色
            TooLar.AutoPopDelay = 10000;//显示时间
            TooLar.RemoveAll();
            TooLar.SetToolTip(myDataGridView, Tall);
            DT.Dispose();
            DtGroup.Dispose();
            DS.Dispose();
            GC.Collect();
            return true;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值