How to compute cells using DataGridView - vb.net

I have 3 column Qty,Price and Amount and i need to compute the amount per line using DataGridView, what are the possible events I may use?

i assumed that Quantity is Cell 0, Price is Cell 1, and Amount is Cell 2
For i As Integer = 0 To DataGridView1.RowCount - 1
Dim xQty as Double = CDBL(DataGridView1.Rows(i).Cells(0).Value)
Dim xPrc as Double = CDBL(DataGridView1.Rows(i).Cells(1).Value)
DataGridView1.Rows(i).Cells(2).Value = xQty * xPrc
Next

Related

I need to add all of the items in a ListBox into a "total" variable

Here is an example of the For loop that I'm trying to run where lstCost is my ListBox that is formed by selecting options from two other ListBoxes and calculating a total for those option to create an item total and totalCost is the variable where I'm trying to save the total of all list items.
Dim totalCost As Integer
For x As Integer = 0 To lstCost.Items.Count - 1
totalCost += Val(lstCost.Items.Item(x))
Next
txtTotalCost.Text = totalCost.ToString
For some reason, I end up with a 0 as my total in my txtTotalCost TextBox. Can anybody think of any reason why?
Dim totalCost As Double = 0
For x As Integer = 0 To lstCost.Items.Count - 1
totalCost += DirectCast(lstCost.Items.Item(x).ToString.trim,Double)
Next
txtTotalCost.Text = totalCost.ToString
depending on your values in listbox you can change it to Decimal if required

Calculating Prices with List Box

Calculating PreTax, Tax, and Total amounts based on items in list box. Here's how it looks currently when executed:
https://drive.google.com/open?id=0B3W0gSES_-fNMmdnU1VTSzR2dFk
Here is the code I have so far:
'load ListBox with test data:
For dblPrices As Double = 1 To 4
lstPrices.Items.Add(dblPrices.ToString("c2"))
Next dblPrices
'calculate pretax total:
Dim dblPretaxTotal As Double = 0
Dim dblSelectedPrice As Double
For intTax As Integer = 0 To lstPrices.Items.Count - 1
lstPrices.SelectedIndex = 0
Dim strPrice As String
strPrice = Convert.ToString(lstPrices.SelectedItem)
Double.TryParse(strPrice, dblSelectedPrice)
dblPretaxTotal = dblSelectedPrice
Next intTax
I only have it programmed to calc and display the PreTax Total currently. It should show $10.00. Any suggestions are welcome.
I think your problem is the failed double conversion.
try this:
dblPretaxTotal += Double.Parse(strPrice, NumberStyles.Currency) * taxRate;
instead of Double.TryParse() without checking for the result. NumberStyles.Currency and a taxrate was also missing.

Randomly select an item from a list based on a class, repeat number of times based on different numbers

I am not familiar with using macro's, but I think that what I would like excel to perform is best handled with a macro. So I can use all the input you may have!
I have these headers;
ID Tag Pen Sex Weight Class Inside range
With 450 rows of data. Based on the distribution of the weight data, I have in two other columns (class and number) the number of rows I want to select within each class. The selected rows must have the value "Yes" in the column "Inside range".
I want to randomly select the rows, based on the number needed for each class, and copy these rows to a new sheet. It sums up to 30 rows in the new sheet.
I hope you have a suggestion how to complete this action!
can you try the following, you will need to add a reference to Microsoft Scripting Runtime library:
Const rowCount = 450
Public Sub copyRows()
Dim i As Integer
Dim j As Integer
Dim classes As Scripting.Dictionary
Dim source As Worksheet
Dim colNumber As Integer
Dim colClassName as Integer
Dim colInsideRange As Integer
Dim allSelected As Boolean
Dim randomRow as Integer
Dim sumRemaining as Integer
allSelected = False
Set source = Worksheets("YourWorksheetName")
colClassName = 6 'this is the column number where class names are entered. I am assuming 6
colNumber = 7 'this is the column number where number of rows to be selected are entered. I am assuming 7
colInsideRange = 8 'this is the column number where "Inside Range" values are entered. I am assuming 9
For i = 2 to rowCount + 1 'assuming you have a header row
classes(CStr(source.Cells(i, colClassName))) = CInt(source.cells(i, colNumber)
Next i
Do until allSelected
Randomize
randomRow = Int ((Rnd * 450) + 2) 'assuming you have a header row, + 1 if you don't
If classes(CStr(source.Cells(randomRow, colClassName))) = 0 Then
With classes
sumRemaining = 0
For j = 1 to .Count - 1
sumRemaining = sumRemaining + .Items(j)
If sumRemaining > 0 Then Exit For
Next j
allSelected = (sumRemaining = 0)
End With
Else
source.Cells(randomRow, colInsideRange) = "Yes"
classes(CStr(source.Cells(randomRow, colClassName))) = classes(CStr(source.Cells(randomRow, colClassName))) - 1
End If
Loop
'Enter your code to copy rows with "Inside Range" = "Yes"
End Sub
Sorry if there are some errors or typos, I wrote from my mobile phone.

Simplified coding for a lot of variables in vba

I have 100 boxes, (box1, box2, etc) each box has 100 rows of values. (0,1,2,etc) So I have a total of 10,000 rows of data. I am counting number of cells in each box with a specific value. I set the range because I can then change the countif value, e.g. number of cells with value of 2, 3, etc.
However, to do this, I have to Dim boxes 100 times and set box= 100 times. Is there a way to simplify this? Each box always has 100 rows, total number of boxes is always 100.
Dim box1 As Range
Dim box2 As Range
Dim box 3 As Range
.....
Set box1 = Range("A1:A100")
Set box2 = Range("A101:200")
Set box3 = Range("A201:300")
.....
Range("C1").formula = "=COUNTIF(box1, "1")"
Range("C2").formula = "=COUNTIF(box2, "1")"
.....
This should get you started in the right direction:
Sub tgr()
Dim box(1 To 100) As Range
Dim i As Long
For i = 1 To UBound(box)
Set box(i) = Cells(100 * (i - 1) + 1, "A").Resize(100)
Cells(i, "C").Formula = "=COUNTIF(" & box(i).Address & ",""1"")"
Next i
End Sub

Adding cell values across a row and display result in another cell

Is it possible to add all the cell values along a row and put result in another column in Datagridview or listview? If yes, how do I go about that in VB.net please?
This is pretty straigtforward. Code if the sum column is the last one:
Dim sumRowIndex As Integer = DataGridView1.ColumnCount - 1
Dim sum As Double = 0
For Each row In DataGridView1.Rows
For collumn = 0 To DataGridView1.ColumnCount - 2
'check if cell value is a number
If Double.TryParse(row.Cells(collumn).Value, Nothing) Then
sum = sum + row.Cells(collumn).Value
End If
Next
row.Cells(sumRowIndex).Value = sum
sum = 0
Next