SUM TEXTBOX VALUES

Sheen Buhay

Total Post:10

Points:70
Posted by  Sheen Buhay
C# 
C#
 6641  View(s)
Ratings:
Rate this:
Hi,

I need to know on how to add multiple values using only one textbox.


Please refer to this screenshot:
http://imageshack.us/photo/my-images/440/breakdown.jpg/

The form has
Descr (TextBox1) fields. The Amount column is our (TextBox2) and Total is our (TextBox3).

The example has 6 entries based on
Descr (Ingredient cost, Tuition Fee, Elect cost, Lab fee, Misc and NCII Assessment) where all entries are taken by textbox2. Each Descr has a corresponding Amount value. So, we need to enter 6 times to satisfy our data entry and display the sum of all values into (textbox3).

Please see illustration :
http://imageshack.us/photo/my-images/819/sumtbvalues.jpg/

Where Amount represents (Ingredient cost, Tuition Fee, Elect cost, Lab fee, Misc and NCII Assessment) costs.


Any help are highly appreciated.


Tags: Visual C#.net, ASP.net, Web Application


Thanks
  1. James Smith

    Post:48

    Points:336
    Re: Sum Textbox Values

    Hi Sheen Buhay,
    Could you please explain, what mean by bind all entries in database.
    Do you like to save all values in a particular table or do you bind all records in datagridview.

    Thanks.

  1. Sheen Buhay

    Post:10

    Points:70
    Re: Sum Textbox Values

    Hi James Smith,

    I probably wasn't clear on my first statement, my apology. Yes, as stated by your previous reply. I would like to save all values in particular table or bind all into a gridview control.

    Please see this link for clearer illustration: http://imageshack.us/photo/my-images/819/sumtbvalues.jpg/

    Thanks.

  1. James Smith

    Post:48

    Points:336
    Re: Sum Textbox Values

    Hi Sheen Buhay,

    You can try following code snippet.

    public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

                //Add columns in datatable

                dt.Columns.Add("Descr");

                dt.Columns.Add("Amount");

            }

            DataTable dt = new DataTable();

     

            private void button1_Click(object sender, EventArgs e)

            {

                DataRow dr = dt.NewRow();  //Create a new row object.

                //Add values in column.

                dr[0] = textBox1.Text;

                dr[1] = textBox2.Text;

                dt.Rows.Add(dr);   //Add rows in table.

                dataGridView1.DataSource = dt.DefaultView;   //Bind record to gridview.

            }

        }

     

    Thanks,

  1. Sheen Buhay

    Post:10

    Points:70
    Re: Sum Textbox Values

    Hi James,

    Please check this link : http://imageshack.us/photo/my-images/819/sumtbvalues.jpg/

    That sample link is a good example for this thread. Adding values and display everything on the grid afterwards sums up all amount in one textbox field (Total).

    Could you please provide me a sample snippet for that.


    Cheers!

  1. Awadhendra Tiwari

    Post:126

    Points:882
    Re: Sum Textbox Values

    Hi Sheen ,
    I am giving you complete sample which full fill your task as u mentioned above.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                dt.Columns.Add("Descr");
                dt.Columns.Add("Amount");
            }
            DataTable dt = new DataTable();
            float totalAmount = 0;   //Declare a variable named totalAmount.
           
            private void button1_Click(object sender, EventArgs e)
            {
                DataRow dr = dt.NewRow();  //Create a new row object.

                //Add values in column.

                dr[0] = textBox1.Text;

                dr[1] = textBox2.Text;

                dt.Rows.Add(dr);   //Add rows in table.

                dataGridView1.DataSource = dt.DefaultView;   //Bind record to gridview.

                if (!string.IsNullOrEmpty(textBox3.Text))
                    textBox3.Text = (Convert.ToDouble(textBox3.Text) + Convert.ToDouble(textBox2.Text)).ToString();  //Add total amount.
                else
                    textBox3.Text =  Convert.ToDouble(textBox2.Text).ToString();   //Add initial amount if empty.
            }
        }
    }


    Thanks,

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!