Hi,
I need to know on how to add multiple values using only one textbox.
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
Anonymous User
14-Oct-2011I 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
14-Oct-2011Please 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!
James Smith
13-Oct-2011Hi Sheen Buhay,
You can try following code snippet.
Thanks,
Sheen Buhay
13-Oct-2011I 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
13-Oct-2011Could 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.