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

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

你可能感兴趣的:(鼠标,tooltip,控件,datagridview,river,digital)