Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have added a gridview in which items are added and a function is called to calculate the amount

after adding a item to list i call

VB
Private Sub totalimsum()
    Dim totalsum As Double = 0
    Dim totalitem As Double = 0

    For i As Integer = 0 To dgvitemlist.Rows.Count - 1
        totalsum += dgvitemlist.Rows(i).Cells("Total").Value
        'totalitem += dgvitemlist.Rows(i).Cells("qty").Value
    Next
    Label16.Text = dgvitemlist.Rows.Count.ToString()
    amttotal = totalsum.ToString()
    txttotal.Text = amttotal
    calctotal()
End Sub


it works perfect but when a row got deleted, I again call the function to recalculate the amount but it miscalculate the amount and I found that rowcount is 1 more than row present in the datagridview so i added a new function and calling it but i think it is not a solution

VB
   Private Sub totalimsumd()
    Dim totalsum As Double = 0
    Dim totalitem As Double = 0

    For i As Integer = 0 To dgvitemlist.Rows.Count - 2
        totalsum += dgvitemlist.Rows(i).Cells("Total").Value
        'totalitem += dgvitemlist.Rows(i).Cells("qty").Value
    Next
    Label16.Text = dgvitemlist.Rows.Count.ToString()
    amttotal = totalsum.ToString()
    txttotal.Text = amttotal
    calctotal()
End Sub


Kindly help to get the solution.
Posted
Updated 4-May-15 22:29pm
v2
Comments
Ralf Meier 5-May-15 3:51am    
Do you delete the row completly or do you only delete the Content of the row ?
That makes a difference ...
yash00121 5-May-15 3:59am    
deleting the row by delete button . i think it is deleting the complete row .
Ralf Meier 5-May-15 4:47am    
Are you doing it like this :

Dim myRow As Integer = DataGrid_Zeiten.CurrentCell.RowIndex
DataGrid_Zeiten.Rows.RemoveAt(myRow)

can you please give me the row delete function ? i think you didn't completely delete the row.
if it is completely delete the count should decrease too.
 
Share this answer
 
Comments
yash00121 5-May-15 5:03am    
i am deleting the row by delete button without any function
yash00121 5-May-15 5:04am    
but now i understood it is just hiding the item not deleting
use the row or cell click event to help store seletedrows
and use selectedrows to be the index to delete the entire row of the data in datagridview like this


C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dgv = sender as DataGridView;
    if (dgv == null)
        return;
    if (dgv.CurrentRow.Selected)
    {
        int selectedrows = 0;
        selectedrows = dgv.CurrentRow.Index;
        dgv.removeat(selectedrows); 
        //do rest you staff.
    }
}

it should work : )
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900