Reference another cell in the same row having calculated the minimum value in a datatable column - vb.net

Using VB, I've used the following line to successfully find the minimum value in a specific column (say column 5, where the values are all of double) in a datatable:
Dim test as double
test = datatable.Compute("min(sourcecolumn)", "")
I would now like to refer to the values in other columns (let's say column 2) along the row containing that minimum column value.
Any help would be much appreciated as I can't get my head round it!
Thanks

You can use the DataTable.Select() method to get the row(s) that contain the minimum value. DataTable.Select() returns a DataRow(). In the code below, I assumed only one column contains the minimum value hence Data(0).
Dim test as double
test = datatable.Compute("min(sourcecolumn)", "")
Dim Data() As DataRow = datatable.Select("sourcecolumn = " & test.ToString())
Dim column2 = Data(0)(1)

All you have is a value but you currently have no idea what row(s) contain that value. You can use the table's Select method to get the row(s) that contain that value in that column. Once you have the row(s), you can do whatever you want with it/them:
Dim minValue = CDbl(myDataTable.Compute("MIN(MyColumn)", Nothing))
Dim rows = myDataTable.Select($"MyColumn = {minValue}")
For Each row In rows
'Use row here.
Next
Select always returns an array, even if there is only one row, so the loop will always work. If you know that there will never be more than one match, you can just get the first element directly.

Related

Insert a value into a row in datagrid vb.net

I'm currently stuck on inserting a value into a row in datagridview in vb.net
My code is:
Dim subtotal As Integer = Val(adult.Text) * 500 + Val(child.Text) * 300
For Each row As DataGridViewRow In DataGridView2.Rows
row.Cells.Insert(subtotal)
Next
It supposed to be like this.
enter image description here
Hint I wanna do it like this
You don't insert a new cell. You get an existing cell in the appropriate column and set its Value property. This line:
row.Cells.Insert(subtotal)
would be like this:
row.Cells(columnIndex).Value = subtotal
That said, that will put the same value in every row. That's probably not what you want but it's all we can you based on the information you've provided. If what you actually expect is to get a different value for each row based on data in that row then the calculation would have to be inside the loop as well, e.g.
Dim adultCount = CInt(row.Cells(adultColumnIndex).Value)
Dim childCount = CInt(row.Cells(childColumnIndex).Value)
Dim subtotal = adultCount * 500 + childCount * 300
row.Cells(subtotalColumnIndex).Value = subtotal
Of course, if you had bound this grid to a DataTable then you could set the Expression property of the DataColumn for the subtotal and it would be calculated automatically.

Reference another cell in a different row having calculated the minimum value in a datatable column

Reference another cell in the same row having calculated the minimum value in a datatable column
The above answered my question as to how to reference another cell in the same row having calculated the minimum value in a datatable column. But how do I reference a cell in another row still using that original cell?
In other words, if the min value in a column is in row 5, I now know how to get the values of cells in all other columns within row 5, but how do I get the values from columns within other rows (eg row 6) using the original cell as a starting point?
Dim Data() As DataRow = datatable.Select("sourcecolumn = " & test.ToString())
Dim column2 = Data(0)(1)
was the answer to my first question, how would I amend this to get to another row? I tried:
dim column2 = Data(1)(1)
but it returns an array error. I think the numbers in brackets are absolute, but I need to use relative ones.
Thanks in advance again.

Select cell from column with row value

This simple problem has caused me some recent issues. I have a range of cells which are columns that hold onto different types of information. Using a row value (Integer not Range) which is previously determined I am looking to perform a check with the values within a single cell.
For example, I look through a list of names in column A. If the name is found it holds onto the Row value. Let's assume that the row is 10. This row value will be used in checking the column values for this row (I.e. C10, J10, and K10). How can I select a single cell and then compare the values within those cells?
To get the equivalent to MATCH() / INDEX() or VLOOKUP() in VBA for getting the data for Darth Vader here:
we could use something like:
Sub GetTheRowValue()
Dim RowValue As Long
RowValue = Range("A:A").Find(What:="Darth Vader", After:=Range("A1")).Row
MsgBox Range("B" & RowValue).Value
End Sub
The finds the proper row and then acquires data from other columns in that row.

Function does not read last value

I am developing a program for a chain of restaurants, they should normally be able to calculate their guestcount and sales through a vb.net application with a connection to dbf files.
My dataset gets filled in correctly as is my datatable (checked by filling in datarowview = right data)
But then I get a problem, in all my functions using my datatable, the datatable skips the final value, in this case it is all values from a month so it either skips 31 or day 30.
Tried while, Tried for each, debugged alot (how i found it was the last value). But now I have no idea why the last value isn't used by the function
Public Function Getgctakeout(ByVal i_table As DataTable)
table = i_table
i = 0
gctakeout = 0
For Each row As DataRow In i_table.Rows
gctakeout = gctakeout + Convert.ToDouble(row(4))
Next row
'MessageBox.Show(gctakeout)
Return gctakeout
End Function
This function does not use the value of the last row to calculate gctakeout
what in the name of the lord is wrong :)
Assuming that your DataTable is really filled correctly, you have two other options to get the sum.
Use the old DataTable.Compute method which works also with .NET < 2.0
Use Linq-To-DatSet and it's Enumerable.Sum
1)
Dim Sum = CType(table.Compute("Sum(ColumnName)", Nothing), Double)
2)
Dim Sum = table.AsEnumerable().
Sum(Function(row)row.Field(Of Double)("ColumnName"))

How can I update a row in a DataTable in VB.NET?

I have the following code:
Dim i As Integer = dtResult.Rows.Count
For i = 0 To dtResult.Rows.Count Step 1
strVerse = blHelper.Highlight(dtResult.Rows(i).ToString, s)
' syntax error here
dtResult.Rows(i) = strVerse
Next
I want to add a strVerse to the current row.
What am I doing wrong?
The problem you're running into is that you're trying to replace an entire row object. That is not allowed by the DataTable API. Instead you have to update the values in the columns of a row object. Or add a new row to the collection.
To update the column of a particular row you can access it by name or index. For instance you could write the following code to update the column "Foo" to be the value strVerse
dtResult.Rows(i)("Foo") = strVerse
You can access columns by index, by name and some other ways:
dtResult.Rows(i)("columnName") = strVerse
You should probably make sure your DataTable has some columns first...
Dim myRow() As Data.DataRow
myRow = dt.Select("MyColumnName = 'SomeColumnTitle'")
myRow(0)("SomeOtherColumnTitle") = strValue
Code above instantiates a DataRow. Where "dt" is a DataTable, you get a row by selecting any column (I know, sounds backwards). Then you can then set the value of whatever row you want (I chose the first row, or "myRow(0)"), for whatever column you want.