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();
}
}
}
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();
}
}
}
Dorababu MekaPosted Nov 16, 2015, 12:42 PM
{
int nRow = dataGridView1.CurrentCell.RowIndex;
if (nRow < dataGridView1.RowCount)
{
for (int i = nRow; i < dataGridView1.RowCount; i++)
{
int rowId = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value);
decimal depense = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value);
decimal Recette = Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value);
DataRow[] rows = dt.Select("ID=" + rowId);
if (i == nRow) rows[0]["Balance"] = depense + Recette;
else
rows[0]["Balance"] = Convert.ToDecimal(dataGridView1.Rows[nRow].Cells[1].Value) + Convert.ToDecimal(dataGridView1.Rows[nRow].Cells[2].Value) + depense + Recette;
}
}
dataGridView1.DataSource = dt;
}
private void button3_Click(object sender, EventArgs e)
{
int nRow = dataGridView1.CurrentCell.RowIndex;
int rowCount = dataGridView1.RowCount;
if (nRow < dataGridView1.RowCount)
{
for (int i = nRow; i < rowCount; i++)
{
int rowId = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value);
decimal depense = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value);
decimal Recette = Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value);
DataRow[] rows = dt.Select("ID=" + rowId);
if (i == nRow) rows[0]["Balance"] = 0;
else
rows[0]["Balance"] = Convert.ToDecimal(dataGridView1.Rows[nRow].Cells[3].Value) + depense + Recette;
}
DataRow[] delrows = dt.Select("ID=" + dataGridView1.SelectedRows[nRow].Cells[0].Value);
delrows[0].Delete();
}
dataGridView1.DataSource = dt;
}
Med AminPosted Nov 16, 2015, 1:51 PM
Dorababu MekaPosted Nov 16, 2015, 1:00 PM
Med AminPosted Nov 16, 2015, 12:54 PM