private void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
 {
     ReadOnlyTextBox roTextBox = (ReadOnlyTextBox)sumBoxHash[dgv.Columns[e.ColumnIndex]];
     if (roTextBox != null)
     {
         if (roTextBox.bSummary)
         {
             calcSummaries();
         }
     }
 }
        /// <summary>
        /// Calculate the Sums of the summary columns
        /// </summary>
        private void calcSummaries()
        {
            foreach (ReadOnlyTextBox roTextBox in sumBoxHash.Values)
            {
                if (roTextBox.bSummary)
                {
                    roTextBox.Tag = 0;
                    roTextBox.Text = "0";
                    roTextBox.Invalidate();
                }
            }
            if (SummaryColumns != null && SummaryColumns.Length > 0 && sumBoxHash.Count > 0)
            {
                foreach (DataGridViewRow dgvRow in dgv.Rows)
                {
                    foreach (DataGridViewCell dgvCell in dgvRow.Cells)
                    {
                        foreach (DataGridViewColumn dgvColumn in sumBoxHash.Keys)
                        {
                            if (dgvCell.OwningColumn.Equals(dgvColumn))
                            {
                                ReadOnlyTextBox sumBox = (ReadOnlyTextBox)sumBoxHash[dgvColumn];

                                if (sumBox != null && sumBox.bSummary)
                                {
                                    if (dgvCell.Value != null && !(dgvCell.Value is DBNull))
                                    {
                                        if (IsInteger(dgvCell.Value))
                                        {
                                            sumBox.Tag = Convert.ToInt64(sumBox.Tag) + Convert.ToInt64(dgvCell.Value);
                                        }
                                        else if (IsDecimal(dgvCell.Value))
                                        {
                                            sumBox.Tag = Convert.ToDecimal(sumBox.Tag) + Convert.ToDecimal(dgvCell.Value);
                                        }

                                        sumBox.Text = string.Format("{0}", sumBox.Tag);
                                        sumBox.Invalidate();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Resize the summary Boxes depending on the 
        /// width of the Columns of the DataGridView
        /// </summary>
        private void resizeSumBoxes()
        {
            try
            {
                this.SuspendLayout();
                if (sumBoxHash != null && sumBoxHash.Count > 0)
                    try
                    {
                        int rowHeaderWidth = dgv.RowHeadersVisible ? dgv.RowHeadersWidth - 1 : 0;
                        int sumLabelWidth = dgv.RowHeadersVisible ? dgv.RowHeadersWidth - 1 : 0;
                        int curPos = rowHeaderWidth;

                        if (DisplaySumRowHeader && sumLabelWidth > 0)
                        {
                            sumRowHeaderLabel.Visible = true;
                            sumRowHeaderLabel.Width = sumLabelWidth;
                            if (dgv.RightToLeft == RightToLeft.Yes)
                            {
                                if (sumRowHeaderLabel.Dock != DockStyle.Right)
                                {
                                    sumRowHeaderLabel.Dock = DockStyle.Right;
                                }
                            }
                            else
                            {
                                if (sumRowHeaderLabel.Dock != DockStyle.Left)
                                {
                                    sumRowHeaderLabel.Dock = DockStyle.Left;
                                }
                            }
                        }
                        else
                        {
                            if (sumRowHeaderLabel.Visible)
                            {
                                sumRowHeaderLabel.Visible = false;
                            }
                        }

                        int iCnt = 0;
                        Rectangle oldBounds;

                        foreach (DataGridViewColumn dgvColumn in SortedColumns)
                        {
                            ReadOnlyTextBox sumBox = (ReadOnlyTextBox)sumBoxHash[dgvColumn];
                            if (sumBox != null)
                            {
                                oldBounds = sumBox.Bounds;
                                if (!dgvColumn.Visible)
                                {
                                    sumBox.Visible = false;
                                    continue;
                                }

                                int from = dgvColumn.Frozen ? curPos : curPos - dgv.HorizontalScrollingOffset;

                                int width = dgvColumn.Width + (iCnt == 0 ? 0 : 0);

                                if (from < rowHeaderWidth)
                                {
                                    width -= rowHeaderWidth - from;
                                    from = rowHeaderWidth;
                                }

                                if (from + width > this.Width)
                                {
                                    width = this.Width - from;
                                }

                                if (width < 4)
                                {
                                    if (sumBox.Visible)
                                    {
                                        sumBox.Visible = false;
                                    }
                                }
                                else
                                {
                                    if (this.RightToLeft == RightToLeft.Yes)
                                    {
                                        from = this.Width - from - width;
                                    }

                                    if (sumBox.Left != from || sumBox.Width != width)
                                    {
                                        sumBox.SetBounds(from, 0, width, 0, BoundsSpecified.X | BoundsSpecified.Width);
                                    }

                                    if (!sumBox.Visible)
                                    {
                                        sumBox.Visible = true;
                                    }
                                }

                                curPos += dgvColumn.Width + (iCnt == 0 ? 0 : 0);
                                if (oldBounds != sumBox.Bounds)
                                {
                                    sumBox.Invalidate();
                                }
                            }
                            iCnt++;
                        }
                    }
                    finally
                    {
                        this.ResumeLayout();
                    }
            }
            #if (DEBUG)
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
                System.Diagnostics.Debug.WriteLine(ee.ToString());
            }

            #else
            catch
            { }
            #endif
        }
        /// <summary>
        /// Create summary boxes for each Column of the DataGridView        
        /// </summary>
        private void reCreateSumBoxes()
        {
            foreach (Control control in sumBoxHash.Values)
            {
                this.Controls.Remove(control);
            }
            sumBoxHash.Clear();

            int iCnt = 0;

            ReadOnlyTextBox sumBox;
            List<DataGridViewColumn> sortedColumns = SortedColumns;
            foreach (DataGridViewColumn dgvColumn in sortedColumns)
            {
                sumBox = new ReadOnlyTextBox();
                sumBoxHash.Add(dgvColumn, sumBox);

                sumBox.Top = 0;
                sumBox.Height = dgv.RowTemplate.Height;
                sumBox.BorderColor = dgv.GridColor;
                sumBox.BackColor = dgv.DefaultCellStyle.BackColor;
                sumBox.ForeColor = dgv.DefaultCellStyle.ForeColor;
                sumBox.BringToFront();

                if (dgv.ColumnCount - iCnt == 1)
                {
                    sumBox.bLastColumn = true;
                }

                if (SummaryColumns != null && SummaryColumns.Length > 0)
                {
                    for (int iCntX = 0; iCntX < SummaryColumns.Length; iCntX++)
                    {
                        if (SummaryColumns[iCntX] == dgvColumn.DataPropertyName || SummaryColumns[iCntX] == dgvColumn.Name)
                        {
                            sumBox.TextAlign = TextHelper.TranslateGridColumnAligment(dgvColumn.DefaultCellStyle.Alignment);
                            sumBox.bSummary = true;
                            sumBox.FormatString = dgvColumn.DefaultCellStyle.Format;

                            if (dgvColumn.ValueType == typeof(System.Int32) || dgvColumn.ValueType == typeof(System.Int16) ||
                                dgvColumn.ValueType == typeof(System.Int64) || dgvColumn.ValueType == typeof(System.Single) ||
                                dgvColumn.ValueType == typeof(System.Double) || dgvColumn.ValueType == typeof(System.Single) ||
                                dgvColumn.ValueType == typeof(System.Decimal))
                            {
                                sumBox.Tag = System.Activator.CreateInstance(dgvColumn.ValueType);
                            }
                        }
                    }
                }

                sumBox.BringToFront();
                this.Controls.Add(sumBox);

                iCnt++;
            }

            sumRowHeaderLabel.Font = new Font(dgv.DefaultCellStyle.Font, SumRowHeaderTextBold ? FontStyle.Bold : FontStyle.Regular);
            sumRowHeaderLabel.Anchor = AnchorStyles.Left;
            sumRowHeaderLabel.TextAlign = ContentAlignment.MiddleRight;
            sumRowHeaderLabel.Height = this.Height;
            sumRowHeaderLabel.Width = dgv.RowHeadersWidth;
            sumRowHeaderLabel.Top = 0;
            sumRowHeaderLabel.Text = DisplaySumRowHeader ? SumRowHeaderText : string.Empty;
            sumRowHeaderLabel.ForeColor = dgv.DefaultCellStyle.ForeColor;
            sumRowHeaderLabel.Margin = new Padding(0);
            sumRowHeaderLabel.Padding = new Padding(0);
            this.Controls.Add(sumRowHeaderLabel);
            calcSummaries();
            resizeSumBoxes();
        }