for each loop using a datagridview - vb.net

I trying to perform a calculation on some values within a DGV that has been imported from data within an xls file. but I cant get the loop to work. what I need it to do is read the value of the cells in column 2 and column 3 of each row, put them in to a text box and then output the result of the calculation to column 4 of the DVG. I would post the code but frankly I haven't got anything real to go on. am I correct in thinking I need to be using a For each loop???
Any help or guidance is greatly appreciated.
Thanks in advance

I find it Little hard understanding your request... But how about this?
For Each Row As DataGridViewRow In DataGridView1.Rows
Dim CellValues As New List(Of String)
Dim Failed As Boolean = False
For x = 1 To 2 'This is right. It's NOT supposed to be 2 To 3.
If Row.Cells(x).Value <> Nothing Then
CellValues.Add(Row.Cells(x).Value.ToString())
ElseIf Row.Cells(x).Value = Nothing Then
Failed = True
End If
Next
If Failed = True Then
Continue For
End If
TextBox1.AppendText(CellValues(0) & " + " & CellValues(1) & Environment.NewLine)
Row.Cells(3).Value = CInt(CellValues(0)) + CInt(CellValues(1))
Next

Related

How to remove a row from DataGridView if data within Total column is Zero

Hello Everyone Good Afternoon
I have a program that imports excel into datagridview but when after I import it the look is like this.
Link of the Image
as what you see in the image above I have a Column Total which composed of NULL(Empty String),0 and a certain data.
I wonder how can I delete the whole row if the column Total has a Data of NULL or 0.
Im using this code and it applies in Datagridview
Private Sub step_3_delete_Zero()
Dim SkipRemove As Boolean
Dim Rowindex As Integer
For Rowindex = DataGridView1.Rows.Count - 1 To 0 Step -1
SkipRemove = False
For Each Cell As DataGridViewCell In DataGridView1.Rows(Rowindex).Cells
If Not Cell.Value.ToString = "0" Then
SkipRemove = True
Exit For
End If
Next
If Not SkipRemove = False Then
DataGridView1.Rows.RemoveAt(Rowindex)
End If
Next
End Sub
I hope someone helps me, TY in advance
In your for each loop where have you said to look in the total cell?
Are you looking in every cell in that row?
Also isn't If SkipRemove = True Then easier to read than If Not SkipRemove = False Then?
I think it's not working because you are looping through and checking each cell rather than looping through by row and then checking the specific cell on each row.
Would something along the lines of this be more easier?
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells.Item("Total").Value = "0" Then
DataGridView1.Rows.Remove(row)
End If
Next

Control name from Variable or Dataset. (Combobox)(.items.add)(.datasource)

I've checked for hours but I can't seem to find anything to help.
I want to loop through tables and columns from a dataset and use the column name in a combobox.items.add() line, however the eventual goal is to fill the combobox from the dataset itself possibly in a combobox.datasource line.
The first problem is that I can't get the code correct to setup the combobox control where it allows me to use .items.add("") and in extension .datasource
Error Message = "Object reference not set to an instance of an object"
dstcopt is the dataset from a oledbDataAdapter .fill(dstcopt,"table") line (which returns correct values)
tc_opt is a tab name on a tab control where the comboboxes are
For Each dstable In dstcopt.Tables
For Each dscolumn In dstable.Columns
Dim colName As String = dscolumn.ToString
MsgBox(colName) 'This retuns "aantigen"
Dim cb As ComboBox = Me.tc_opt.Controls("cb_" & colName)
cb.Items.Add(colName)
'cb_aantigen.DataSource = dstcopt.Tables(dstable.ToString)
'cb_aantigen.DisplayMember = "aantigen"
'cb_atarget.DataSource = dstcopt.Tables(dstable.ToString)
'cb_atarget.DisplayMember = "atarget"
Next
Next
The second problem comes when I do it manually (which works) using the exact combobox names cb_aantigen and cb_atarget as seen in the comments.
The problem is that once the form is loaded and the cb's are filled with the correct values, I can't change the value in any single cb individually, when I change one value it changes them all (there is 15 comboboxes in total) I know this is down to using a dataset, but I don't know away to 'unlink them from each other or the dataset'
Not sure if I need to split this into 2 questions, but help on either problem would be appreciated.
EDIT:
After looking at only this section of code for a day. This is what I have come up with to tackle both the problems at once.
The combobox control not working was down to using a tab tc_opt instead of a groupbox gp_anti
The issue with splitting the dataset up into individual comboboxes, I've worked around by taking the value of each cell in the database and adding it separately, probably a better way to do it though
For Each dstable As DataTable In dstcopt.Tables
For Each dscolumn As DataColumn In dstable.Columns
Dim colName As String = dscolumn.ToString
Dim cb(2) As ComboBox
cb(0) = CType(Me.gp_anti.Controls("cb_" & colName), ComboBox)
cb(1) = CType(Me.gp_rec.Controls("cb_" & colName), ComboBox)
cb(2) = CType(Me.gp_nat.Controls("cb_" & colName), ComboBox)
For icb = 0 To cb.Count - 1
If Not (IsNothing(cb(icb))) Then
For irow = 0 To dstable.Rows.Count - 1
If dstable.Rows(irow)(colName).ToString <> Nothing Then
Dim icbitemdupe As Boolean = False
If cb(icb).Items.Contains(dstable.Rows(irow)(colName).ToString) Then
icbitemdupe = True
End If
If icbitemdupe = False Then
cb(icb).Items.Add(dstable.Rows(irow)(colName).ToString)
End If
End If
Next
End If
Next
Next
Next

Search Datagridview Vb.net

I have been stuck in creating search for Datagridview in Vb.net.
I have one DataGridView that is bound to binding source. It contains data such as:
123456,
213926,
285643,
395687,
I have searched but everywhere but I only found a filter method for binding source or a find method.
The filter method removes remaining rows & find method finds an exact string.
I found a method to find text in DataGridView but that search found the string anywhere in DataGridView column like if user type 2 then it will first select the row having 2 such as 123456.
I want to create search that should find letter in sequence from start & so on.
If the user presses 2 then search should go for cell starting with 2 such as 213926.
Matter solved Frank. Thankyou So Much.
Here is my complete code for others too.
Dim Found As Boolean = False
Dim StringToSearch As String = ""
Dim ValueToSearchFor As String = Me.TextBox1.Text.Trim.ToLower
Dim CurrentRowIndex As Integer = 0
Try
If dgvDishonourReceipts.Rows.Count = 0 Then
CurrentRowIndex = 0
Else
CurrentRowIndex = dgvDishonourReceipts.CurrentRow.Index + 1
End If
If CurrentRowIndex > dgvDishonourReceipts.Rows.Count Then
CurrentRowIndex = dgvDishonourReceipts.Rows.Count - 1
End If
If dgvDishonourReceipts.Rows.Count > 0 Then
For Each gRow As DataGridViewRow In dgvDishonourReceipts.Rows
StringToSearch = gRow.Cells(4).Value.ToString.Trim.ToLower
If InStr(1, StringToSearch, LCase(Trim(TextBox1.Text)), vbTextCompare) = 1 Then
Dim myCurrentCell As DataGridViewCell = gRow.Cells(4)
Dim myCurrentPosition As DataGridViewCell = gRow.Cells(0)
dgvDishonourReceipts.CurrentCell = myCurrentCell
CurrentRowIndex = dgvDishonourReceipts.CurrentRow.Index
Found = True
End If
If Found Then Exit For
Next
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try

Removing row DataGridView from x to y

So as the question say, is it possible to delete row from a to b I've tried a couple things like this one
Dim dgv As DataGridViewRowCollection = DataGridView1.Rows
For Each rw As DataGridViewRow In dgv
Console.WriteLine(rw.Index)
If (rw.Index > iNbrRow) Then
If (Not rw.IsNewRow) Then
Console.WriteLine("Delete : " & rw.Index)
'dgv.RemoveAt(rw.Index)
End If
End If
Next
But the problem is every time a row is deleted the index change so it doN,t delete all of them
P.S : iNbrRow = number of row - 2 who's initialized at form load
Thankcx hope you guys can help :S
This won't happen if you start at the end of the gridview and go backward:
For i As Integer = gView1.Rows.Count-1 To 0 Step -1
If i > iNbrRow then
' delete the row
end if
Next i

VB.NET excel deleting multiple columns at the same time

I am trying to delete more than one column in my excel sheet.
For Each lvi In ListView1.Items
If lvi.Checked = True Then
arrayLetters = lvi.SubItems(1).Text & ":" & lvi.SubItems(1).Text & "," & arrayLetters
End If
Next
arrayLetters = arrayLetters.Substring(0, arrayLetters.Length - 1)
Dim rg As Excel.Range = xlSheet.Columns(arrayLetters)
rg.Select()
rg.Delete()
The value of arrayLetters is "G:G,F:F". For some reason that doesn't seem to work once it gets there to delete them! Only reason i am doing it this way is so that it doesn't update the table and loop to the other one. In other words, if i delete each one individually then the column moves and the letter will not be the same the next go around.
The error is on the Dim rg As Excel.Range = xlSheet.Columns(arrayLetters) line and it says:
Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
Any help would be great!
David
SOLVED
For Each lvi In ListView1.Items
If lvi.Checked = False Then
arrayLetters = lvi.SubItems(2).Text & "," & arrayLetters 'Puts numbers in BACKWORDS
End If
Next
arrayLetters = arrayLetters.Substring(0, arrayLetters.Length - 1)
Dim theNumbers As String() = arrayLetters.Split(",")
Dim num As Integer = 0
xlApp.ScreenUpdating = False
For Each num In theNumbers
xlSheet.Columns(num).delete() 'Deletes columns in reverse order (7,5,4...)
Next
Just delete the lowest numbered column N number of times to reflect how many columns in a row you want to delete. It's better to go off of column numbers rather than letters when dealing with Excel programatically. If you need a code example, let me know and I'll post one.
Edit:
Here is a code example that does what you want:
xlSheet.Columns(i).delete
You want
xlSheet.Range(arrayLetters)
I reckon, that is Range, not Column.
I think the important part to realize (which I didn't initially) is that you have to use numbers for the columns not the letters.