Home > DeveloperSection > Forums > Sum Textbox Values
Sheen Buhay

Total Post:10

Points:70
Posted on    October-12-2011 6:25 PM

 C# C# 
Ratings:


 6 Reply(s)
 5905  View(s)
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


James Smith

Total Post:48

Points:336
Posted on    October-13-2011 12:39 AM

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.

Sheen Buhay

Total Post:10

Points:70
Posted on    October-13-2011 1:01 AM

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.

James Smith

Total Post:48

Points:336
Posted on    October-13-2011 3:40 AM

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,


Sheen Buhay

Total Post:10

Points:70
Posted on    October-14-2011 10:47 PM

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!

Awadhendra Tiwari

Total Post:126

Points:882
Posted on    October-14-2011 11:01 PM

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,

Sheen Buhay

Total Post:10

Points:70
Posted on    October-24-2011 3:41 AM


Don't want to miss updates? Please click the below button!

Follow MindStick