How can i get the overall total amount in the datagridview to a textbox in a same form?
// this part is calculating the total amount of a row
private void SalesDataGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (SalesDataGrid != null)
{
for (int x = 0; x < SalesDataGrid.Rows.Count; x++)
{
SalesDataGrid.Rows[x].Cells[3].Value = Convert.ToInt32(SalesDataGrid.Rows[x].Cells[1].Value) *
Convert.ToDouble(SalesDataGrid.Rows[x].Cells[2].Value);
//and i've tried this one below if it will work but it didn't do anything
}
if (SalesDataGrid != null)
{
for (int x = 0; x < SalesDataGrid.Rows.Count; x++)
{
txtsalestotal.Text = SalesDataGrid.Rows[x].Cells[3].Value.ToString() ;
}
}
Try This:
double totalAmount=0;
for (int x = 0; x < SalesDataGrid.Rows.Count; x++)
{
totalAmount += Convert.ToDouble(SalesDataGrid.Rows[x].Cells[3].Value.ToString());
}
txtsalestotal.Text=totalAmount.ToString();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.