Data Grid View Replace Value - vb.net

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.

Related

Condition to Continue a For Loop

First go around with programming in VB.net and came across a little issue. I'm trying to find a way to have an If statement control whether a for loop executes or not before going through another iteration.
Code:
For Each theTable In tableDoc.Tables
testString = ""
For row = 1 To theTable.Rows.Count
isHeaderOrNot = theTable.Cell(row, 1).Range.Text 'Gets value in first column
If isHeaderOrNot is Not Like "Section" or "Field" then Continue For 'if the words "Section" or "Field" are NOT included in isHeaderOrNot then continue with For loop
keyText = theTable.Cell(row, 2).Range.Text
valueText = theTable.Cell(row, 3).Range.Text
Console.WriteLine("KEY: {0}", keyText)
Console.WriteLine("VALUE: {0}", valueText)
Console.WriteLine("")
Next
Next
I'm getting an error on Like saying there's an expression expected..
Basically if the first column in the row the for loop is in contains Section or Field I want it go to the next line. First time trying to explain a question dealing with this, so any questions just ask! I truly appreciate your time!
You can just include the whole block in the if, and if it's not the case and you want to end the loop, you can use Exit For.
For Each theTable In tableDoc.Tables
testString = ""
For row = 1 To theTable.Rows.Count
isHeaderOrNot = theTable.Cell(row, 1).Range.Text
'If cell doesn't contain Section or Field, then do the following
If Not isHeaderOrNot.contains("Section") AndAlso Not isHeaderOrNot.contains("Field")
keyText = theTable.Cell(row, 2).Range.Text
valueText = theTable.Cell(row, 3).Range.Text
Console.WriteLine("KEY: {0}", keyText)
Console.WriteLine("VALUE: {0}", valueText)
Console.WriteLine("")
Else
'If it DOES include Section or Field, then stop looping
Exit For
End If
Next
Next
The correct syntax should be
If Not isHeaderOrNot Like "Section" AndAlso
Not isHeaderOrNot Like "Field" Then
Exit For

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

datagridview value already exist vb

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.

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 check if the cell value is nothing or not in vb.net?

i am using grid view in vb.net.
here is code...
If Not DataGridView1.SelectedRows.Count = 0 Then
i = DataGridView1.SelectedRows(0).Index
If DataGridView1.Rows(i).Cells(0).Value <> Nothing Then
namebox.Text = Trim(DataGridView1.Rows(i).Cells(0).Value)
salarybox.Text = DataGridView1.Rows(i).Cells(1).Value
End If
End If
now if the cell has nothing in it then it will show the exception....
like this...
Operator '<>' is not defined for type 'DBNull' and 'Nothing'.
this is code will be called when selected cell will be changed.
i trying to get the values of the selected cell and put that in in one text box.
You don't want to use the <> operator, you should us value IsNot Nothing to check if it IsNot Nothing or inversely Is Nothing to check if a value Is Nothing.
Also the reason is that there is no comparer for the DBNull and Nothing Types so if this is the case you will need to check to see for both. Something like
If value IsNot Nothing AndAlso value <> DBNull.Value Then
''#Do something
End If
Change
If DataGridView1.Rows(i).Cells(0).Value <> Nothing Then
to
If Not DataGridView1.Rows(i).Cells(0).Value Is DBNull.Value Then