Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
there is an error after deleting row in dgv. i deleted it manually using delete button on keyboard.

this problem when appear when deleting all rows.
if there are 3 row, i delete 1 row - no problem,
delete 3 - this is problem

but if there only one row - the problem appear.


C#
private void beliDtlDataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
    {
        e.Row.Cells["qty"].Value = "0";
        e.Row.Cells["harga"].Value = "0";
        e.Row.Cells["discp"].Value = "0";
        e.Row.Cells["disc"].Value = "0";
        e.Row.Cells["total"].Value = "0";

    }

    private void beliDtlDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        int iColumn = beliDtlDataGridView.CurrentCell.ColumnIndex;
        int iRow = beliDtlDataGridView.CurrentCell.RowIndex;

        if (iColumn == 2)
        {
            if (iRow >= 0 && !MultiSatuan)
            {
                try
                {
                    beliDtlDataGridView.CurrentCell = beliDtlDataGridView.Rows[iRow].Cells[4];
                    SendKeys.Send("{up}");
                }
                catch (InvalidOperationException ex)
                {
                    return;
                }
            }
            else
            {
                try
                {
                    beliDtlDataGridView.CurrentCell = beliDtlDataGridView.Rows[iRow].Cells[3];
                    SendKeys.Send("{up}");
                }
                catch (InvalidOperationException ex)
                {
                    return;
                }

            }
        }
        else
        {
            //new line
            if (iColumn == 8)
            {
                try
                {
                    beliDtlDataGridView.CurrentCell = beliDtlDataGridView.Rows[iRow + 1].Cells[0];
                }
                catch (InvalidOperationException ex)
                {
                    return;
                }
            }
            else
            {
                SendKeys.Send("{up}");
                SendKeys.Send("{right}");
            }

        }
        Hitung();

    }


    private void Hitung()
    {
        if (beliDtlDataGridView.Rows.Count > 1)
        {
            var result2 = beliDtlDataGridView.Rows.Cast<DataGridViewRow>()
            .Where(r => r.Cells["total"].Value != null)
            .Sum(r => Double.Parse(r.Cells["total"].Value.ToString()));

            this.grandTextBox.Text = string.Format("{0:N00}", result2.ToString());
            this.discTextBox.Text = (Convert.ToDecimal(MyTools.IsNull(this.discpTextBox.Text)) * Convert.ToDecimal(result2.ToString()) / 100).ToString();
            this.dppTextBox.Text = (Convert.ToDecimal(result2.ToString()) - Convert.ToDecimal(MyTools.IsNull(discTextBox.Text))).ToString();
            this.pPnTextBox.Text = (Convert.ToDecimal(MyTools.IsNull(dppTextBox.Text)) / 10).ToString();
            this.totalTextBox.Text = (Convert.ToDecimal(MyTools.IsNull(dppTextBox.Text)) + Convert.ToDecimal(MyTools.IsNull(pPnTextBox.Text))).ToString();
        }
        else
        {
            this.grandTextBox.Text = "0";
            this.discTextBox.Text = "0";
            this.dppTextBox.Text = "0";
            this.pPnTextBox.Text = "0";
            this.totalTextBox.Text = "0";
        }
    } 
Posted
Updated 19-Sep-13 19:20pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900