Adding cell values across a row and display result in another cell - vb.net

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

Related

How to hide duplicate rows in a DataGridView?

I have a DataGridView in which I have transferred rows from another Form.
In my DataGridView I have three columns: the first one is name, the second is value, the third is id.
I want to hide duplicate rows with the same name and id.
How can I do this? My code is not working.
For i As Integer = 1 To Form.DataGridView1.Rows.Count - 1
If Form.DataGridView1.Rows(i).Cells(0).Value.ToString = Form.DataGridView1.Rows(i - 1).Cells(0).Value.ToString AndAlso
Convert.ToInt32(Form.DataGridView1.Rows(i).Cells(2).Value) = Convert.ToInt32(Form.DataGridView1.Rows(i - 1).Cells(2).Value) Then
Form.DataGridView1.Rows(i).Visible = False
End If
Next
You have only compared current row with current index-1 row. So, your code is not working. You need a nested loop to eliminate duplicate rows:
For i As Integer = 1 To Form.DataGridView1.Rows.Count - 1
For j As Integer = i - 1 To 0 Step -1
If Form.DataGridView1.Rows(i).Cells(0).Value.ToString = Form.DataGridView1.Rows(j).Cells(0).Value.ToString AndAlso
Convert.ToInt32(Form.DataGridView1.Rows(i).Cells(2).Value) = Convert.ToInt32(Form.DataGridView1.Rows(j).Cells(2).Value) Then
Form.DataGridView1.Rows(i).Visible = False
End If
Next
Next
Here is output

Last column always shows null in Datagridview in vb.net

In my datagridview, I'm entering the values in each column. On button click I'm calculating these values using some formula an populating in another datagridview.
Here I have four columns in first Datagridview, but when I press button, I can get first three columns value only. When I see in watch window, the last column value shows as nothing.
If I delete fourth column and only having three columns now, the third column value shows as nothing.
How can I avoid this?
My coding
For iRow = 0 To LogCalcEnter.Rows.Count - 1
If (Trim(LogCalcEnter.Rows(iRow).Cells(0).Value) <> "" And Trim(LogCalcEnter.Rows(iRow).Cells(1).Value) <> "" And Trim(LogCalcEnter.Rows(iRow).Cells(2).Value) <> "") Then
Dim sw As Decimal
sw = LogCalcEnter.Rows(iRow).Cells(2).Value
RowIndex = LogCalValue.Rows.Add()
Dim NewRow As New DataGridViewRow
NewRow = LogCalValue.Rows(RowIndex)
NewRow.Cells(0).Value = fA.Text * fRW.Text / ((sw ^ fM.Text)) ^ (1 / fN.Text)
NewRow.Cells(1).Value = LogCalcEnter.Rows(iRow).Cells(1).Value
NewRow.Cells(2).Value = LogCalcEnter.Rows(iRow).Cells(2).Value * LogCalcEnter.Rows(iRow).Cells(1).Value
NewRow.Cells(3).Value = NewRow.Cells(0).Value * NewRow.Cells(1).Value
End If
Next iRow
Here LogCalcEnter.Rows(iRow).Cells(2).Value always return nothing.

Getting RowIndex based on the multiple selected cells on a DataGridGiew

I'm trying to get the row indices based on my multiple selected cells on a DataGridView. How can I do that in VB.NET?
This is what I have:
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
sno.Text = row.Cells("F2").Value.ToString
sname.Text = row.Cells("F3").Value.ToString
final.Text = row.Cells("F16").Value.ToString
End If
DatagridView has a SelectedCells propertry and a RowIndex on each cell:
For Each c As DataGridViewCell In DataGridView1.SelectedCells
Debug.Print(c.RowIndex)
Next
You will get a hit for each selected cell - possibly with duplicate rRowIndex.
You can set the SelectionMode to rows and use SelectedRows.
To use the first selected cell as the row to use:
If DataGridView1.SelectedCells.count > 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(DataGridView1.SelectedCells(0).RowIndex)
sno.Text = row.Cells("F2").Value.ToString
sname.Text = row.Cells("F3").Value.ToString
final.Text = row.Cells("F16").Value.ToString
End If

Get row index if some column value is equal to something

In this datatable there are no duplicates, I need the row index where column x value equals 2. I would do it like this:
Dim rowIndex As Integer = 0
For i = 0 To mtable.Rows.Count - 1
If mtable.Rows(i)("x") = 2 Then
rowIndex = i
Exit For
End If
Next
I will be calling this process multiple times per second. Is there a faster way to do this in .NET?
DataTable select could work, i think it should be faster than iterating over the collection of rows.
var index = mtable.Rows.IndexOf(mtable.Select("x = 2").FirstOrDefault());
Multiple times per second is a bit vague - tens or thousands?
You could create a hash table mapping the value of "x" to the row number:
Dim nLookups = mtable.Rows.Count - 1
Dim lookupHash As New Hashtable(nLookups)
For i = 0 To nLookups
lookupHash.Add(CInt(mtable.Rows(i)("x")), i)
Next
then
Dim rowSought As Integer = -1
If lookupHash.ContainsKey(2) Then
rowSought = lookupHash(2)
End If
Or if the range of possible values of "x" is suitable, you could use an array to map the value to the row number.

How to compute cells using DataGridView

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