Use formula for multiplication? - vb.net

I have two fields on a record ("Qty" and "Harga").
How do I multiply the two and save result into another field in a ListView?

The ListView control has no built-in ability to perform calculations for you, like a spreadsheet. It just displays whatever data you give it to display. If you want it to display the product of a multiplication equation, you will need to do that calculation yourself in the code and then add the result to the ListView column. For instance:
Public Sub AddItem(description As String, total As Integer, count As Integer)
Dim i As ListViewItem = ListView1.Items.Add(description)
i.SubItems.Add(total.ToString())
i.SubItems.Add(count.ToString())
Dim product As Integer = total * count
i.SubItems.Add(product.ToString())
End Sub

Related

Can I use a method that returns a list of strings in SSRS report code as the headers in a tablix?

I have table that needs to contain 50 columns for each half hour in the day (+2 for daylight savings). So each column will be HH1, HH2, HH3... HH50.
I have written this piece of code in the report properties code section.
Function GetHH() As List(Of String)
Dim headers As List(Of String) = new List(Of String)
For index As Integer = 1 to 50
headers.Add("HH" & index)
Next
return headers
End Function
Is there a way to use the output of this function as the headers of my tablix? Or will I need to add the headers to some sort of dataset in the database and add it from there?
The column group functionality would be well suited for this. As you mentioned, you would need to write a SQL statement to return these values in a dataset. Then you can set your column group to group on these values. This way your table always gets the right number of columns and you don't have to add them manually.

Averaging numerous values in a class

I have a class with a lot of parameters as it takes in all the columns in Excel, since this is the first step I usually do when building a macro. Then I started to think about how to actually accomplish what I'm trying to do and I think I just wasted a couple of hours of coding.
What I am trying to do is average each of the parameters inside the class as well as trying to get minimum and maximum values. There are about 100000 rows in these excel files.
My plan was to put everything into a collection and then I was thinking there was going to be some sort of averaging function or a min and max function for the collection parameter, or a way to send an array to a function that can calculate min or max.
What would be ideal:
Dim collectionOfRecords as new collection
for each row in sheet.rows
Dim r as New RmmRecord
call r.PopulateClass(row)
collectionOfRecords.add r
next row
Then get the average from the collection somehow like this:
dim parameter1Average as double
parameter1Average = collectionOfRecords.Parameter1.Average '(ha, I wish)
OR:
parameter1average = GetAverageFromCollection(collectionOfRecords, Parameter1)
Public Function GetAverageFromCollection(records as Collection, parameterToAverage as something??)
for each record in records
sum = record.parameterToAverage + sum
next record
GetAverageFromCollection = sum / records.count
end function
Thank you!

Using a LINQ query to populate a combo box with data from an access database

In VB.Net:
I am trying to populate a combo box on my form using data from a MS Access database. Specifically; taking all of the last names from the database, sorting them in ascending order in an output list which I named players and then adding each item in players to my combo box (cboPlayer).
Public Sub GetPlayers()
Dim PlayerLastName As New List(Of String)
PlayerLastName.Add("Smith")
PlayerLastName.Add("Hill")
PlayerLastName.Add("Beyer")
PlayerLastName.Add("Wilson")
PlayerLastName.Add("Hudson")
PlayerLastName.Add("van Zegeren")
PlayerLastName.Add("Bibbs")
PlayerLastName.Add("Muller")
PlayerLastName.Add("Pierce")
PlayerLastName.Add("Henry")
PlayerLastName.Add("Johnston")
Dim Players = From Last In PlayerLastName
Order By Last Ascending
Select Last
cboPlayer.Items.Add(Players.ToString)
cboPlayer.SelectedItem = 0
I know this isn't exactly correct but not positive what direction to head in. When I run the program the combo box is populated with System.linq.enumerated.
Any ideas what that might mean or what I am doing incorrectly?
Maybe over thinking it just a little? Why not try without the linq, List has a sort function. Also just bind PlayerLastName to the combobox.
Public Sub GetPlayers()
Dim PlayerLastName As New List(Of String)
PlayerLastName.Add("Smith")
PlayerLastName.Add("Hill")
PlayerLastName.Add("Beyer")
PlayerLastName.Add("Wilson")
PlayerLastName.Add("Hudson")
PlayerLastName.Add("van Zegeren")
PlayerLastName.Add("Bibbs")
PlayerLastName.Add("Muller")
PlayerLastName.Add("Pierce")
PlayerLastName.Add("Henry")
PlayerLastName.Add("Johnston")
PlayerLastName.Sort()
cboPlayers.DataSource = PlayerLastName
cboPlayers.SelectedIndex = -1
End sub
Edit:
or if you still want to us linq then, you need to change the linq return of IEnumerable to a List...
Dim Players = From Last In PlayerLastName
Order By Last Ascending
Select Last
ComboBox1.DataSource = Players.ToList
ComboBox1.SelectedIndex = -1

Visual Basic Data Grid View - Count Average

Okay, so I've got a datagrid view DataGridView1 which is something like the below example.
Name Points
Jack 15
zack 19
Cody 05
I want to be able to count the average of all of the points, However the amount of points will be dynamic and change from time to time. So my solution must be able to work even if there's just two numbers and when there's upwards of 20.
I have been looking a round for a way of doing this, but most posts are only relevant if the amount of 'points' are static. And a lot of the solutions appear to be written in C++, which isn't too much use to a newbie coder like myself on Visual Basic.
So could anyone help me out here?
I would force it to draw a nice extra line in the data grid, by using a union select and a blanks for each column in the sqldatabind. This would add a nice row to the dataset be formatted as a totals row.. You may need to number the rows so your totals row is last most because of ordering. add the term 'Total' as a control in the results or handle it another way.
After that its just a bit of tweaking when your grid draws the row..
something like ...
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim dRow As GridViewRow = sender
If dRow.RowType = DataControlRowType.DataRow Then
If dRow.Cells(0).Text = "Total" Then
Dim rx As Integer
Dim TotalValue As Double = 0
For rx = 0 To GridView1.Rows.Count - 2
TotalValue += CDbl(GridView1.Rows(rx).Cells(1).Text)
Next
dRow.Cells(1).Text = FormatNumber(TotalValue, 2)
End If
End If
End Sub

VB.net Can't get output to appear in my listbox. Beginners Question

Trying to get the user to put 3 numbers in 3 text boxes and get the average.
Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAverage.Click
Dim a As Integer = CInt(txtone.Text)
Dim b As Integer = CInt(txtTwo.Text)
Dim c As Integer = CInt(txtThree.Text)
Dim average As Integer
average = (a + b + c) / 3
lstOutput.Text = average
End Sub
Try changing the type of average from Integer to Double
Dim average as Double
Right now you're trying to store the Average in an Integer which can only hold a whole number. Averages tend to be non-whole numbers and need a data type that can represent that. Double is good for most situations. That should fix your problem.
EDIT OP mentioned that lstOutput is a ListBox
This is one of the confusing things with WinForms. Even though every single control has a Text property, not all of them actually do anything. They only apply to elements that directly display a single text block or value. Ex Button, Label, etc ...
A ListBox on the other hand displays a group of items. You want to add a new item to the list.
lstOutput.Items.Add(average.ToString())
The Text property of a list box will get or set the selected item. You haven't added your average to the listbox yet.
Try:
lstOutput.Items.Add(average)
Are you sure that txtOne.text txtTwo.text and txtThree.txt will always be an integer value?
You might need to also change the a,b,c vars to Doubles and check that the user didn't supply non-numeric values.
If the user puts "one" in the txtOne textbox, you'll get an exception kablowee.
(air coding here)
dim a as new double
try
if isnumeric(txtOne.text.tostring.trim) then
a = cdbl(txtOne.text.tostring.trim)
end if
'repeat for b and c ...
catch ex as exception
messagebox.show(ex.message.tostring)
end try
And, I'm not sure if I'm right about this, (maybe someone will enlighten me) but does .NET consider type conversion from string to int differently in these two cases
a = cint(txtOne.text)
and
a = cint(txtOne.text.tostring)
???