TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Med Amin
NA
16
9.4k
Calculte Value DataGridView
Nov 10 2015 10:29 AM
I have a DataGridView like this :
ID
Depense
Recette
Balance
1
5
5
10
2
15
15
40
With id, Depense, Recette and Balance.
Balance is calculated by add Balance , Depence and Recette.
I have 3 button, AddProduct,UpdateProduct and DeleteProduct.
* AddProduct : it will add a line into the DataGridView, but if the DataGridView is
empty
, (1st line )Balance = 0+5+5, and if isn't, (2nd line) Balance = 10+15+15=40,
* UpdateProduct: I f I update a line, it will affect the other data, example, if I upadet the 1st line with Depense = 10, so the Balance in 1st will be 15 and the 2nd line will be 45.
* DeleteProduct : also the same idea with UpdateProduct's button but just remove the Balance from affected lines.
I code this part :
I modify the code and I call it when I click o button for test, the 1st click works perfect, but when I click again it add again sum, so I add another 'for' to calculate the balance for every row alone, but nothing happen, it stays the same problem
Decimal BalanceInput=0;
int IdTreInput;
private void UpdateBalance()
{
// calculate again somme of balance
for (int i = 0; i < DataGridViewTresorerie.Rows.Count; i++)
{
BalanceInput = Convert.ToDecimal(DataGridViewTresorerie.Rows[i].Cells[11].Value) + Convert.ToDecimal(DataGridViewTresorerie.Rows[i].Cells[12].Value);
IdTreInput = Convert.ToInt32(DataGridViewTresorerie.Rows[i].Cells[1].Value);
using (SqlConnection con = new SqlConnection(connstring))
{
using (SqlCommand cmd = new SqlCommand("Update Tresorerie set Balance= @Balance WHERE IdTre = @IdTre", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Balance", BalanceInput);
cmd.Parameters.AddWithValue("@IdTre", IdTreInput);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
// update balance
for (int i = 0; i < DataGridViewTresorerie.Rows.Count; i++)
{
if (i == 0)
BalanceInput = Convert.ToDecimal(DataGridViewTresorerie.Rows[i].Cells[13].Value);
else
{
BalanceInput += Convert.ToDecimal(DataGridViewTresorerie.Rows[i].Cells[13].Value);
IdTreInput = Convert.ToInt32(DataGridViewTresorerie.Rows[i].Cells[1].Value);
using (SqlConnection con = new SqlConnection(connstring))
{
using (SqlCommand cmd = new SqlCommand("Update Tresorerie set Balance= @Balance WHERE IdTre = @IdTre", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Balance", BalanceInput);
cmd.Parameters.AddWithValue("@IdTre", IdTreInput);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
//I call the new DataGridView to recalculate again after modify or add or delete the new data
BindGrid();
}
}
}
Reply
Answers (
4
)
WCF hosted in IIS 8 is not working
How to Saveas xlsx file with file format as DBF4.