how to calculate the gridview rows and show the answer into text box - sql

i am trying to calculate the gridview rows value and want to show in textbox.
I have 5 rows in gridview , itemid , itemname,orderedqty,price,totalprice. when ever I click add the rows here, I want to calculate total amount of totalprice. want to show it to textbox.
what I was trying this is the code ,I think it will not work. please help me to find solution.
Public Sub Calculate()
Dim Countrow As Integer = 1
Dim AmountTotal As Decimal = 0
For Each row As DataGridView In dgvPurchaseOrder.Rows
Countrow += 1
AmountTotal = dgvPurchaseOrder.Rows.Count.ToString()
Next
'GridView1.FooterRow.Cells(1).Text = "Total = " & AmountTotal.ToString()
txtGrossTotal.Text = AmountTotal.ToString()
End Sub

Try Like This
Public Sub Calculate()
Dim Countrow As Integer = 1
Dim AmountTotal As Decimal = 0
For Each row As DataGridView In dgvPurchaseOrder.Rows
Countrow += 1
AmountTotal += row.Cells(4).Value // give correct price column index
Next
txtGrossTotal.Text = AmountTotal.ToString()
End Sub

Public Sub Calculate()
Dim Countrow As Integer = 1
Dim AmountTotal As Decimal = 0
For Each row As DataGridViewRow In dgvPurchaseOrder.Rows
Countrow += 1
AmountTotal += row.Cells(4).Value
Next
txtGrossTotal.Text = AmountTotal.ToString()
End Sub
#equisde you are the hero, this is the correct code.

Related

How to get Total of Column in DataGridView in vb.net

I used the below code and it's rounding the result
Dim iTax As Double
If VacationsDataGridView.RowCount > 1 Then
For index As Double = 0 To VacationsDataGridView.RowCount - 1
iTax += Convert.ToDouble(VacationsDataGridView.Rows(index).Cells(3).Value)
Next
Label4.Text = iTax
End If
I don't know what's the mistake!

prevent go to next row when duplicated value in `datagridview`

I have datagrid that user will add values in just one column , and i want to prevent duplicated data in this column, i had mange that by the code bellow,
what I want is to keep the selection (focus) in the same editing cell(x)
if the entered data is duplicated, so I tried to get the current row index and return to it if the data is duplicated but its not working.
row_index = DataGridView1.CurrentRow.Index.ToString()
Dim cell As DataGridViewCell = DataGridView1.Rows(row_index).Cells(1)
DataGridView1.CurrentCell = cell
DataGridView1.BeginEdit(True)
NOTE : User will Add barcode number, so he will use barcode scanner or add it manually and press enter key.
Private Sub DataGridView1_RowValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowValidated
If DataGridView1.Rows.Count > 2 Then
Dim i As Integer = 0
Dim row_index As Integer
' loop condition will loop while the row count is less or equal to i
While i <= DataGridView1.Rows.Count - 1
Dim j As Integer = 1
' loop condition will loop while the row count is less or equal to j
While j <= DataGridView1.Rows.Count - 1
Dim str As String = DataGridView1.Rows(i).Cells(1).Value()
Dim str1 As String = DataGridView1.Rows(j).Cells(1).Value()
If Not str1 = "" AndAlso Not str = "" Then
If str1 = str Then
'row_index = DataGridView1.SelectedCells.Item(i).RowIndex.ToString()
row_index = DataGridView1.CurrentRow.Index.ToString()
Dim cell As DataGridViewCell = DataGridView1.Rows(row_index).Cells(1)
DataGridView1.CurrentCell = cell
DataGridView1.BeginEdit(True)
Exit Sub
End If
End If
j += 1
End While
i += 1
End While
End If
End Sub
You can try it also in a for loop. This is how I do it. If what you mean is to stop/retain the selection(focus) or go to in a cell/row whenever it is a duplicate with the current data you have. This should work fine.
enter code here
For i = 0 To Datagridview1.Rows.Count - 1
If Datagridview1.Rows(i).Cells(1).Value = DataYouWillInput Then
DataGridView1.CurrentCell = DataGridView1.Rows(i).Cells(1)
Exit for
End If
Next
enter code here
If your condition returns true with the condition of current data and data you'll be adding. Just exit your loop. Use this.
DataGridView1.CurrentCell = DataGridView1.Rows(i).Cells(1)

Datagridview not showing correct rowcount

I made a gridview where items are added and a function is called to calculate the amount
after adding a item to list i call
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 is 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
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
Try rebuilding the data source of the DataGridView which means you reload the contents of the DataGridView.
Or
You may try to use the dgv.update(); and dgv.refresh(); methods whenever you delete a row.

Search DataGridView for Integer Then Select Row

The below code should search DataGridView1 which is on the LeaderAccessTable form for an integer that the user inputs into SendFromID, if the DataGridView1's first column contains what the integer that the user has entered into SendFromID then the entire row should be selected. However it doesn't select any rows at all... Can anyone see why? This code is ran from a separate form.
Dim intcount As Integer
For Each Row As DataGridViewRow In LeadersAccessTable.DataGridView1.Rows
If LeadersAccessTable.DataGridView1.Rows(intcount).Cells(0).Value.ToString = SendFromID.Text Then
LeadersAccessTable.DataGridView1.Rows(intcount).Selected = True
End If
Next
MsgBox("Done.")
In the end this code worked.
Dim v_SelectRow As Integer
For counter = 0 To (LeadersAccessTable.DataGridView1.Rows.Count - 1)
For counter2 = 0 To (LeadersAccessTable.DataGridView1.Columns.Count - 1)
If (LeadersAccessTable.DataGridView1.Rows(counter).Cells(0).Value.ToString.Contains(SendFromID.Text)) Then
LeadersAccessTable.DataGridView1.Rows(counter).Cells(0).Selected = True
v_SelectRow = LeadersAccessTable.DataGridView1.CurrentRow.Index
CurrentPoints.Text = LeadersAccessTable.DataGridView1.Item(8, v_SelectRow).Value
'Do Something
Else
'Do Something
End If
Next
Next

VB.NET: How to dynamically select a list view item?

I need to dynamically select an item in a listview based on what was selected previously.
The items that have been selected in the past are retrieved from a database and added to an Arraylist. These items then need to be selected from a number of different listviews.
Doing this by index like so listRef1.Items(2).Checked = True is no problem but I need to do it by the item text, i.e. one of the strings in the array.
So far I have this:
For i As Integer = 0 To refsArr.Count - 1
'find the correct category id
Dim cmdRefCat As New SqlCommand("SELECT RefID from ReferencesListTable WHERE RefName = '" & refsArr(i) & "'", conn)
Dim refid As Integer = cmdRefCat.ExecuteScalar()
If refid = 1 Then
listRef1.Items(refsArr(i)).Checked = True
ElseIf refid = 2 Then
listRef2.Items(refsArr(i)).Selected = True
listRef2.Select()
ElseIf refid = 3 Then
listRef3.Items.Item(refsArr(i)).Selected = True
listRef2.Select()
ElseIf refid = 4 Then
listRef4.Items.Item(refsArr(i)).Selected = True
End If
Next
Has anyone got any ideas on this? Thanks.
You'll need to loop through each item in the listview list:
For I as Integer = 0 to ListView.Items.Count - 1 Do
If ListView.Items(i).Text = "Text" then
ListView.Items(i).Selected = true
End If
End For
You can try this ...
For i As Integer = 0 To refsArr.Count - 1
'find the correct category id
Dim cmdRefCat As New SqlCommand("SELECT RefID from ReferencesListTable WHERE RefName = '" & refsArr(i) & "'", conn)
Dim refid As Integer = cmdRefCat.ExecuteScalar()
Select case refid
case 1
CheckIt(refsArr(i),listRef1)
case 2
CheckIt(refsArr(i),listRef2)
case 3
CheckIt(refsArr(i),listRef3)
case 4
CheckIt(refsArr(i),listRef4)
End Select
Next
And Sub CheckIt
Sub CheckIt(ByVal sRef as String, ByRef lvw as Listview)
Dim x as Integer
For x = 0 to lvw.Items.Count - 1
If lvw.Items(x).Text = sRef then
lvw.Items(x).Selected = true
exit for '-- if only 1 record
End If
Next
End Sub
The code to select an item dynamically from the listview control can be as follows for vb.net.
Let lvwomominiChair1 is the name of the listview control.
Set its fullrowselect property as true.
The code will select the text in the first column of the listview control.
Private Sub lvwomominiChair1_Click(sender As Object,e As EventArgs) Handles lvwomominiChair1.Click
Dim lvwitem as ListViewItem
lvwitem = lvwomominiChair1.SelectedItems.Item(0)
MsgBox("Selected item is " + lvwitem.Text)
End Sub
There may be situations where we need to get all items in a row of a ListView control.The following code may be used for the purpose.It is assumed that there are five columns of data in a raw and are of the text data type.This can be done with a For..Next loop as follows.Let 0,1,2,3 and 4 are the five column indices.
Private Sub lvwomominiChair1_Click(sender As Object,e As EventArgs) Handles lvwomominiChair1.Click
Dim i As Int32
Dim str As String
str =""
For i =0 To 4
str = str + " " + lvwomominiChair1.SelectedItems(0).SubItems(i).Text
Next
MsgBox("Selected items of the five columns of the row are " + str)
End Sub
Or you can do this, works perfect for me:
ListView.Items(0).Selected = True
ListView.Select()