calculating cell values and suming up in datagridview

srm2010 0 Tallied Votes 438 Views Share
Here I am Sending you my code ,wherein I want to have total of examfees(column),PromotionFees(column),Totalfees(column) and Balance(column).Examfees are paid in installment in my project

The idea if single record is found then it doing calculation of balance=promotionfees-totlafees

and if anywhere due to mistyping is value is higher then the respectively msgbox displays error.For single record it is working fine even when I update values in Datagridview.balance shown correctly.

But issues is when there are multirecords:when I am doing total of all the row value in promotionfees(column) in totalfees(column) now balance also should show correctly that is happening fine.But when I update any of the values in promotionfees or Totalfees currentrow level Balance not shown properly.It gets sets for all the rows of Balance column.

What I want when I change any value in any of the column any row the Balance should show correctly.

My data goes like this:

ExamFees   PromotionFees  TotalFees(Feespaid)  Balance

5000            4500                3000                          1500               

 5000          0000                 1000                              500

5000         0000                     500                           0000

The total is shown 4500     4500    0000

whenever I change a single record in totalfees automatically Balance should show for that row as well total value should be calculated and displayed in textbox.

Code:

private void RecptMasterGridvw_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            decimal exmfees = 0;
            decimal Promofees = 0;
            decimal feespd = 0;
            decimal balfees = 0;

            decimal promoval = 0;
            decimal feesval = 0;
            decimal balval = 0;

            exmfees = System.Convert.ToDecimal(RecptMasterGridvw.CurrentRow.Cells["ExamFees"].Value);
            Promofees = System.Convert.ToDecimal(RecptMasterGridvw.CurrentRow.Cells["PromotionFees"].Value);
            feespd = System.Convert.ToDecimal(RecptMasterGridvw.CurrentRow.Cells["TotalFees"].Value);
            balfees = System.Convert.ToDecimal(RecptMasterGridvw.CurrentRow.Cells["Balance"].Value);

//for single records that works fine
            if (RecptMasterGridvw.RowCount == 1)
            {

                balfees = Promofees - feespd;
                RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = balfees;

                if (Promofees > exmfees)
                {


                    MessageBox.Show("Promotion Can not be More than ExamFees", "Promotion Update Error");

                    //dataGridView1.Rows[e.RowIndex].ErrorText = "the value can not  be a greater ";
                }
                if (feespd > Promofees)
                {
                    MessageBox.Show("Fees paid Can not be More than Promotion", "Promotion Update Error");

                    //dataGridView1.Rows[e.RowIndex].ErrorText = "the value can not  be a greater ";

                }
                decimal negetivebalnce = 0;
                negetivebalnce = Convert.ToDecimal(RecptMasterGridvw.CurrentRow.Cells["Balance"].Value);
                if ((negetivebalnce < 0) && (promoval > feesval))
                {
                    balval = promoval - feesval;
                    RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = balval;
                }
                else if (negetivebalnce < 0)
                {
                    negetivebalnce = 0;
                    RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = negetivebalnce;
                }


                txtpromotot.Text = Promofees.ToString();
                txttotalpaid.Text = feespd.ToString();
                txtbalamt.Text = balfees.ToString();
            }

//for multi records

            else if (RecptMasterGridvw.RowCount > 1)
            {



                for (int i = 0; i <= RecptMasterGridvw.RowCount - 1; i++)
                {
                    promoval = promoval + Convert.ToDecimal(RecptMasterGridvw.Rows[i].Cells["PromotionFees"].Value);
                    feesval = feesval + Convert.ToDecimal(RecptMasterGridvw.Rows[i].Cells["TotalFees"].Value);
                    balval = balval + Convert.ToDecimal(RecptMasterGridvw.Rows[i].Cells["Balance"].Value);


                }

                if (promoval > exmfees)
                {

                    //dataGridView1.Rows[e.RowIndex].ErrorText = "the value can not  be a greater ";
                    MessageBox.Show("Promotion Can not be More than ExamFees", "Promotion Update Error");


                }


                if (feesval > promoval)
                {
                    // dataGridView1.Rows[e.RowIndex].ErrorText = "Can not be greater than promotion ";
                    MessageBox.Show("Fees paid can not be More than  Promotion fees", "Paid Fees Update Error");

                }



                // else
                if (feesval == promoval)
                {
                    balval = 0;

                    RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = balfees;
                    txtbalamt.Text = balfees.ToString();
                }
                decimal negetivebal = 0;
                negetivebal = Convert.ToDecimal(RecptMasterGridvw.CurrentRow.Cells["Balance"].Value);
                if ((negetivebal < 0) && (promoval > feesval))
                {
                    balval = promoval - feesval;
                    RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = balval;
                }
                else if (negetivebal < 0)
                {
                    negetivebal = 0;
                    RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = negetivebal;
                }
                balval = promoval - feesval;

                // RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = balfees;
                txtpromotot.Text = promoval.ToString();
                txttotalpaid.Text = feesval.ToString();
                txtbalamt.Text = balval.ToString();



            }





        }
Here I am Sending you my code ,wherein I want to have total of examfees(column),PromotionFees(column),Totalfees(column) and Balance(column).Examfees are paid in installment in my project

The idea if single record is found then it doing calculation of balance=promotionfees-totlafees

and if anywhere due to mistyping is value is higher then the respectively msgbox displays error.For single record it is working fine even when I update values in Datagridview.balance shown correctly.

But issues is when there are multirecords:when I am doing total of all the row value in promotionfees(column) in totalfees(column) now balance also should show correctly that is happening fine.But when I update any of the values in promotionfees or Totalfees currentrow level Balance not shown properly.It gets sets for all the rows of Balance column.

What I want when I change any value in any of the column any row the Balance should show correctly.

My data goes like this:

ExamFees   PromotionFees  TotalFees(Feespaid)  Balance

5000            4500                3000                          1500               

 5000          0000                 1000                              500

5000         0000                     500                           0000

The total is shown 4500     4500    0000

whenever I change a single record in totalfees automatically Balance should show for that row as well total value should be calculated and displayed in textbox.

Code:

private void RecptMasterGridvw_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            decimal exmfees = 0;
            decimal Promofees = 0;
            decimal feespd = 0;
            decimal balfees = 0;

            decimal promoval = 0;
            decimal feesval = 0;
            decimal balval = 0;

            exmfees = System.Convert.ToDecimal(RecptMasterGridvw.CurrentRow.Cells["ExamFees"].Value);
            Promofees = System.Convert.ToDecimal(RecptMasterGridvw.CurrentRow.Cells["PromotionFees"].Value);
            feespd = System.Convert.ToDecimal(RecptMasterGridvw.CurrentRow.Cells["TotalFees"].Value);
            balfees = System.Convert.ToDecimal(RecptMasterGridvw.CurrentRow.Cells["Balance"].Value);

//for single records that works fine
            if (RecptMasterGridvw.RowCount == 1)
            {

                balfees = Promofees - feespd;
                RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = balfees;

                if (Promofees > exmfees)
                {


                    MessageBox.Show("Promotion Can not be More than ExamFees", "Promotion Update Error");

                    //dataGridView1.Rows[e.RowIndex].ErrorText = "the value can not  be a greater ";
                }
                if (feespd > Promofees)
                {
                    MessageBox.Show("Fees paid Can not be More than Promotion", "Promotion Update Error");

                    //dataGridView1.Rows[e.RowIndex].ErrorText = "the value can not  be a greater ";

                }
                decimal negetivebalnce = 0;
                negetivebalnce = Convert.ToDecimal(RecptMasterGridvw.CurrentRow.Cells["Balance"].Value);
                if ((negetivebalnce < 0) && (promoval > feesval))
                {
                    balval = promoval - feesval;
                    RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = balval;
                }
                else if (negetivebalnce < 0)
                {
                    negetivebalnce = 0;
                    RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = negetivebalnce;
                }


                txtpromotot.Text = Promofees.ToString();
                txttotalpaid.Text = feespd.ToString();
                txtbalamt.Text = balfees.ToString();
            }

//for multi records

            else if (RecptMasterGridvw.RowCount > 1)
            {



                for (int i = 0; i <= RecptMasterGridvw.RowCount - 1; i++)
                {
                    promoval = promoval + Convert.ToDecimal(RecptMasterGridvw.Rows[i].Cells["PromotionFees"].Value);
                    feesval = feesval + Convert.ToDecimal(RecptMasterGridvw.Rows[i].Cells["TotalFees"].Value);
                    balval = balval + Convert.ToDecimal(RecptMasterGridvw.Rows[i].Cells["Balance"].Value);


                }

                if (promoval > exmfees)
                {

                    //dataGridView1.Rows[e.RowIndex].ErrorText = "the value can not  be a greater ";
                    MessageBox.Show("Promotion Can not be More than ExamFees", "Promotion Update Error");


                }


                if (feesval > promoval)
                {
                    // dataGridView1.Rows[e.RowIndex].ErrorText = "Can not be greater than promotion ";
                    MessageBox.Show("Fees paid can not be More than  Promotion fees", "Paid Fees Update Error");

                }



                // else
                if (feesval == promoval)
                {
                    balval = 0;

                    RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = balfees;
                    txtbalamt.Text = balfees.ToString();
                }
                decimal negetivebal = 0;
                negetivebal = Convert.ToDecimal(RecptMasterGridvw.CurrentRow.Cells["Balance"].Value);
                if ((negetivebal < 0) && (promoval > feesval))
                {
                    balval = promoval - feesval;
                    RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = balval;
                }
                else if (negetivebal < 0)
                {
                    negetivebal = 0;
                    RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = negetivebal;
                }
                balval = promoval - feesval;

                // RecptMasterGridvw.CurrentRow.Cells["Balance"].Value = balfees;
                txtpromotot.Text = promoval.ToString();
                txttotalpaid.Text = feesval.ToString();
                txtbalamt.Text = balval.ToString();



            }





        }