datagridview value already exist vb - vb.net

this is my code for checking if a value(from dgvlist) already exist in dgvorders, my problem is the error message only shows for the first selectedrow of the dgvlist and the rest still adds.
For Each row As DataGridViewRow In dgvOrders.Rows
For i = 0 To dgvOrders.Rows.Count - 1
If row.Cells(0).Value = dgvList.SelectedRows(i).Cells(0).Value Then
MsgBox("item already listed!", MsgBoxStyle.Critical)
Else : dgvOrders.Rows.Add(v, w, x, y, z)
End If
Exit Sub
Exit For
Next
Next

Because after If statement you are using End sub so once the first record is compared, It is coming out of entire sub.

I suggest you focus on each item in turn. You only want one For Each ... Next loop (unless you also wanted to traverse the columns).
Next get the value to test into a variable ValueFromdgvList. It should be declared as a number or string to match the value expected. I don't know if I have the right syntax or the right value in the code below. You should put a breakpoint in your code and step it so you can check that you have the expected value before starting your compare.
Finally to add a row you need to set the values to an array. Better would be to define a new DataGridViewRow and add the values to that. Finally add it to the DataGridView. But I'll leave that.
'change this as needed
Dim ValueFromdgvList As String = dgvList.SelectedRows(0).Cells(0).Value
Debug.Print(ValueFromdgvList)
Dim haveMatch As Boolean
For Each row As DataGridViewRow In dgvOrders.Rows
If row.Cells(0).Value = ValueFromdgvList Then
haveMatch = True
Exit For
End If
Next
If haveMatch Then
MsgBox("item already listed!", MsgBoxStyle.Critical)
Else
Dim ValuesArray() As Object = {v, w, x, y, z}
dgvOrders.Rows.Add(ValuesArray)
End If
We're using haveMatch because we don't want to add a new row until we've checked all the rows.

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

Show only checked boxes in DataGridView

I am returning SQL results into a DataGridView and have run into a problem. I have an option on my form to only show checked values but can't get it working. Here's my code:
For x As Integer = dgvAutogrow.Rows.Count - 1 To dgvAutogrow.Rows.Count
If dgvAutogrow.Rows(x).Cells("checked").Value = False Then
dgvAutogrow.Rows.Remove(dgvAutogrow.Rows(x))
End If
Next
Here's what part of my DataGridView looks like. I want an event to occur that shows only values that have the check box checked.
When I debug I get the following error:
Any suggestions on what to change?
You have to cast the cell as DataGridViewCheckBoxCell and then test the value like this:
For x As Integer = dgvAutogrow.Rows.Count - 1 To 0 Step -1
Dim cel as DataGridViewCheckBoxCell
cel = CType(dgvAutogrow.Rows(x).Cells("YourColumnName"), DataGridViewCheckBoxCell)
If cel.Value = False Then
dgvAutogrow.Rows.Remove(dgvAutogrow.Rows(x))
End If
Next
Also, you had an ArgumentException because the column you specified doesn´t exist, you need to change that.
Code is tried and tested.
You can try this. We need to cast the cell as a DataGridViewCheckBoxCelland change your loop...
For x As Integer = dgvAutogrow.Rows.Count - 1 To 0 Step -1
If CType(dgvAutogrow.Rows(x).Cells("checked"), DataGridViewCheckBoxCell).Value = False Then
dgvAutogrow.Rows.Remove(dgvAutogrow.Rows(x))
End If
Next

HeaderText vs Design Name of Column in DataGrid

I have the following code which works fine, but it gets the design name (DataGridViewTextBoxColumn11) of my column instead of the header text. How can I alter my code to get the header text on my column instead of the design name?
For Each row As DataGridViewRow In grdTransaction.Rows
If grdTransaction.Item("DataGridViewTextBoxColumn11", row.Index).Value IsNot Nothing Then
If grdTransaction("DataGridViewTextBoxColumn11", row.Index).Value.Equals(DBNull.Value) Then
row.DefaultCellStyle.BackColor = Color.Red
End If
End If
Next
My convention for this kind of thing is to name the item as the database column name prefixed by dgc (DataGridColumn). Thus the heading is 'Customer Name' and the columnname is 'dgcCustomerName'. If you have more than one datagrid on the form or user control, and you have (for instance) CustomerId in columns in both grids, naming them the same creates a conflict. dgcCustomerId is an object in the container namespace, allowing you to get property settings from it without having to specify the grid membership first. That being the case, in the second grid you might have to call the column 'dgcInvoiceCustomerId' or 'dgcLineItemCustomerId'.
Create a function to return the column index from the DGV from supplied column header text:
Private Function getColID(ByVal colHeaderText As String) As Integer
Dim result As Integer = -1
For Each col As DataGridViewColumn In grdTransaction.Columns
If col.HeaderText = colHeaderText Then
result = col.Index
Exit For
End If
Next
return result
End Function
then in your code:
For Each row As DataGridViewRow In grdTransaction.Rows
If grdTransaction.Item(getColID("header text"), row.Index).Value IsNot Nothing Then
If grdTransaction(getColID("header text"), row.Index).Value.Equals(DBNull.Value) Then
row.DefaultCellStyle.BackColor = Color.Red
End If
End If
Next
or this little change:
For Each row As DataGridViewRow In grdTransaction.Rows
If row.cells(getColID("header text")).Value IsNot Nothing Then
If row.cells(getColID("header text")).Value.Equals(DBNull.Value) Then
row.DefaultCellStyle.BackColor = Color.Red
End If
End If
Next
I think this is simple ..
For Each row As DataGridViewRow In grdTransaction.Rows
If Not IsDBNull(row.cells("column_name").Value) Then
row.DefaultCellStyle.BackColor = Color.Red
End If
Next

How to add items to listbox when a loop is involved?

Dim intX, intY As Integer
intY = Nums.GetUpperBound(0)
For intX = 0 To intY
With Nums(intX)
If .strFtID = strID Then
‘calls various subs/functions to get results to show in listbox
listbox.Items.Add(String.Format(strFmt, "various titles”))
listbox.Items.Add(String.Format(strFmt, variable results))
End If
End With
Next
In this loop the listbox for titles is added for every match but I only want it to be added once. I also want to add “no match” if after the entire loop has searched and comes up with no match. There are multiple matches in this loop so it can’t be placed within it or under “else”.
To be able to know if a match was found I usually add a boolean value outside the loop that I call "found". I then set it to false. If the if statement ever is a match i set found to true inside the if. This way when the loop ends I will know if there was a match or not.
Dim found as Boolean = false
For
If
found = true
End if
Next
For the listbox I would do this:
If Not listbox.Items.Contains(String.Format(strFmt, "various titles”)) Then
listbox.Items.Add(String.Format(strFmt, "various titles”))
End if
Try this code, I think this will suits your requirement.
Dim intX, intY As Integer
intY = Nums.GetUpperBound(0)
For intX = 0 To intY
With Nums(intX)
If .strFtID = strID Then
'This following If statement will restricts the duplicate entries, That is
'multiple matches in your words.
If Not listbox.Items.Contains(String.Format(strFmt, "various titles”)) Then
listbox.Items.Add(String.Format(strFmt, "various titles”))
listbox.Items.Add(String.Format(strFmt, variable results))
End if
End If
End With
Next
Now After the loop, just check the count of the listbox. If its count is greater than zero, then some matches had beed found in the upper loop. so that we can leave with out taking any further actions, Other wise just add the word "No Matches" in that listbox, refer the code below,
if listbox.Items.count > 0
listbox.items.add(" NO MATCHES FOUND ")
end if

Data Grid View Replace Value

Can you help me with this..
For Each r As DataGridViewRow In dglist.Rows
For Each c As DataGridViewCell In r.Cells
If c.Value IsNot Nothing Then
**If c.Value.ToString = Label2.Text.ToString Then
c.Value.ToString.Trim.Replace(c.Value.ToString.Trim, TextBox1.Text)
End If**
'MessageBox.Show(c.Value.ToString)
End If
Next
Next
I need to replace the value of the cell with the value of the label.
But still my code did not work.
You need to assign the value (to cell value property) rather than operate on the value of string you have. When you do an operation like ToString etc an additional instance in memory is created and that's all if you are not assigning it. Do this instead:
For Each r As DataGridViewRow In dglist.Rows
For Each c As DataGridViewCell In r.Cells
If c.Value IsNot Nothing Then
**If c.Value.ToString = Label2.Text Then
c.Value = TextBox1.Text
End If**
'MessageBox.Show(c.Value.ToString)
End If
Next
Next
Reconsider your if clauses since I am not very sure that's what you are looking for. In your question you say you want to assign the value of label, but in the code you pasted it seemed to me that you are after text value of your text box.