Can someone tell what's wrong with that code? - vb.net

this button is to check if there is an empty cells in the last row .
if there is filled cell the button (confirmer) will be enabled .. the application crashes when i debug
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
DataGridView1.AllowUserToAddRows = "true"
DataGridView1.ClearSelection()
DataGridView1.Rows(DataGridView1.NewRowIndex).Selected = True
Dim c As Boolean
Dim D As String
Dim nbrcell As Integer = DataGridView1.CurrentRow.Cells.Count - 1
c = Confirmer.Enabled
Do
For i As Integer = 0 To nbrcell - 1
D = DataGridView1.SelectedRows(0).Cells(i).Value
If D <> "" Then
c = True
End If
Next
Loop Until c = True
End Sub

I have changed your code like this:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
DataGridView1.AllowUserToAddRows = "true"
DataGridView1.ClearSelection()
DataGridView1.Rows(DataGridView1.NewRowIndex).Selected = True
Dim c As Boolean
Dim D As Object
Dim nbrcell As Integer = DataGridView1.CurrentRow.Cells.Count - 1
c = Confirmer.Enabled
Do
For i As Integer = 0 To nbrcell - 1
D = DataGridView1.SelectedRows(0).Cells(i).Value
If Not IsDBNull(D) Then
c = True
End If
Next
Loop Until c = True
End Sub
Hope it helps

You are trying to read the number of cells from the CurrentRow property.
But if there is no CurrentRow set when you run this code then the code crashes with a Null Reference Exception. (A.K.A. Object Reference not set to an instance of an object)
Instead you should use the row at NewRowIndex.
Dim nbrcell As Integer = DataGridView1.Rows(DataGridView1.NewRowIndex).Cells.Count
and don't subtract 1 from the count of cells. You are doing this two times. One when you get the Count and one more time when you loop over that count (So the last cell is not checked)
Now the loop: You never exit from that loop if the cells are all filled
Dim D as Object
Dim allFilled = True
For i As Integer = 0 To nbrcell - 1
D = DataGridView1.SelectedRows(0).Cells(i).Value
If D Is Nothing OrElse
Convert.IsDBNull(D) OrElse
String.IsNullOrWitheSpace(D.ToString()) Then
allFilled = False
Exit For ' no need to continue the loop
End If
Next
Confirmer.Enabled = allFilled
I have removed the external while, the the loop is executed only using the for and it is stopped as soon as a cell with Nothing, DBNull or empty string value is found. At that point a boolean variable is set to false and applied to the Enabled property of the button. If no cells are found to be invalid then the boolen variable remains set to true and the button will be enabled.
Probably it is possible to change something in the evaluation code of the cell, but this depends on how do you fill the grid (manually or through a database data)

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Confirmer.Visible = True
Confirmer.Enabled = False
DataGridView1.AllowUserToAddRows = "true"
DataGridView1.ClearSelection()
DataGridView1.Rows(DataGridView1.NewRowIndex).Selected = True
Dim c As Boolean = False
Dim D As Object
Dim nbrcell As Integer = DataGridView1.CurrentRow.Cells.Count
Do
For i As Integer = 0 To nbrcell - 1
D = DataGridView1.Rows(DataGridView1.NewRowIndex).Cells(i).Value
If Not IsDBNull(D) Then
Confirmer.Enabled = True
c = True
End If
Next
Loop Until c = True
End Sub
The Problem now is that the button(confirmer) is always enabled even is the cell is empty

Related

Validate more than 3 cells in Datagridview VB.net

How can I validate 4 entered cells if they exist in the same datagridview, at the time of writing in the 4 cell cell
So people my datagriview
Sub listar_CC_configurados()
dtc = Negcc.Listar_CC_configurados(VGlobales.Base)
For Each data As DataRow In dtc.Rows
Dim aa As Integer = Me.dgvlistccconfigurados.Rows.Add()
'Me.dgv1.Rows(aa).Cells(0).Value = data("ACCION").ToString().Trim
Me.dgvlistccconfigurados.Rows(aa).Cells(0).Value = data("IDSUCURSAL").ToString().Trim
Me.dgvlistccconfigurados.Rows(aa).Cells(1).Value = data("SUCURSAL").ToString()
Me.dgvlistccconfigurados.Rows(aa).Cells(2).Value = data("IDALMACEN").ToString()
Me.dgvlistccconfigurados.Rows(aa).Cells(3).Value = data("ALMACEN").ToString()
Me.dgvlistccconfigurados.Rows(aa).Cells(4).Value = data("IDCC").ToString()
Me.dgvlistccconfigurados.Rows(aa).Cells(5).Value = data("CC").ToString()
Me.dgvlistccconfigurados.Rows(aa).Cells(6).Value = data("PERIODO_INICIO").ToString()
Me.dgvlistccconfigurados.Rows(aa).Cells(7).Value = data("PERIODO_FIN").ToString()
Next
End Sub
In the CellEndit event, I perform the validation only of cell No. 4 as follows
Private Sub dgvlistccconfigurados_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvlistccconfigurados.CellEndEdit
Dim clave As String
Dim nlinea As Integer
If e.ColumnIndex = 4 Then
clave = dgvlistccconfigurados.Rows(dgvlistccconfigurados.CurrentRow.Index).Cells(4).Value.ToString
nlinea = dgvlistccconfigurados.CurrentRow.Index
For i As Integer = 0 To dgvlistccconfigurados.Rows.Count - 1
If clave = dgvlistccconfigurados.Rows(i).Cells(4).Value.ToString And i <> nlinea Then
dgvlistccconfigurados.Rows(nlinea).Cells(4).Value = ""
MsgBox("esta repetido el codigo")
SendKeys.Send("{UP}")
Exit Sub
End If
Next
End If
End Sub
End Class
Now I can't find a way to validate the 4 cells you enter if they exist in the same datagridview, I can only do it with the 4 cell.
Any idea what I would be missing to perform this requirement?
I am applying the following code but it gives me an error
Private Sub dgvlistccconfigurados_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvlistccconfigurados.CellEndEdit
Dim clave As String
Dim suc, almacen As String
Dim nlinea As Integer
If e.ColumnIndex = 4 Then
clave = dgvlistccconfigurados.Rows(dgvlistccconfigurados.CurrentRow.Index).Cells(4).Value.ToString
suc = dgvlistccconfigurados.Rows(dgvlistccconfigurados.CurrentRow.Index).Cells(0).Value.ToString
almacen = dgvlistccconfigurados.Rows(dgvlistccconfigurados.CurrentRow.Index).Cells(2).Value.ToString
nlinea = dgvlistccconfigurados.CurrentRow.Index
For i As Integer = 0 To dgvlistccconfigurados.Rows.Count - 1
If Convert.ToString(clave And suc And almacen) = Val(dgvlistccconfigurados.Rows(i).Cells(4).Value And dgvlistccconfigurados.Rows(i).Cells(0).Value And dgvlistccconfigurados.Rows(i).Cells(2).Value) And i <> nlinea Then
dgvlistccconfigurados.Rows(nlinea).Cells(4).Value = ""
MsgBox("esta repetido el codigo")
SendKeys.Send("{UP}")
Exit Sub
End If
Next
End If
End Sub
Conversion of string to type 'Long' is invalid.

copy the data from unbound dgv to bound dgv

I have two dgv, the first dgv is unbound and the second is bound. I want to copy the data in every cell to the bound dgv in order to save it. I have button but when I click it it says "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."
Here is my code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For i As Integer = 0 To DataGridView1.Rows.Count() - 1 Step +1
Dim rowAlreadyExist As Boolean = False
Dim check As Boolean = DataGridView1.Rows(i).Cells(3).Value
Dim row As DataGridViewRow = DataGridView1.Rows(i)
If check = True Then
If Sales.SalesDataGridView.Rows.Count() > 0 Then
For j As Integer = 0 To Sales.SalesDataGridView.Rows.Count() - 1 Step +1
If row.Cells(0).Value.ToString() = Sales.SalesDataGridView.Rows(j).Cells(0).ToString() Then
rowAlreadyExist = True
Exit For
End If
Next
If rowAlreadyExist = False Then
Sales.SalesDataGridView.Rows.Add(row.Cells(0).Value.ToString(),
row.Cells(1).Value.ToString(),
row.Cells(2).Value.ToString(),
row.Cells(3).Value.ToString(),
row.Cells(4).Value)
End If
Else
Sales.SalesDataGridView.Rows.Add(row.Cells(0).Value.ToString(),
row.Cells(1).Value.ToString(),
row.Cells(2).Value.ToString(),
row.Cells(3).Value.ToString(),
row.Cells(4).Value)
End If
End If
Next

Need help comparing a string to a listbox with a loop

Code is below. I'm trying to compare a user input string with a large listbox and if it finds a match, it terminates the loop and prints a response. I keep getting hung up with it freezing or printing the wrong response.
Private Sub btnAction_Click(sender As Object, e As EventArgs) Handles btnAction.Click
Dim input As String = txtIn.Text
Dim i As Integer = 0
While i <= lstRoseBowl.Items.Count - 1
If input = CStr(lstBox.Items(i)) Then
txtOut.Text = "Yes"
Else
txtOut.Text = "No"
End If
End While
End Sub
You need to increment "i"
Private Sub btnAction_Click(sender As Object, e As EventArgs) Handles btnAction.Click
Dim input As String = txtIn.Text
Dim i As Integer = 0
While i <= lstRoseBowl.Items.Count - 1
If input = CStr(lstBox.Items(i)) Then
txtOut.Text = "Yes"
Else
txtOut.Text = "No"
End If
i += 1 ' <-----
End While
End Sub
Or better yet, use a for loop
Private Sub btnAction_Click(sender As Object, e As EventArgs) Handles btnAction.Click
Dim input As String = txtIn.Text
For i As Integer = 0 To lstRoseBowl.Items.Count - 1
If input = CStr(lstBox.Items(i)) Then
txtOut.Text = "Yes"
Else
txtOut.Text = "No"
End If
Next
End Sub
Now, this will compile and run but might not give you the result you want since if it find the items on the first try, it would then display No on the second try. There are better ways of doing it but for the least amount of code change, it could look like this.
Private Sub btnAction_Click(sender As Object, e As EventArgs) Handles btnAction.Click
Dim input As String = txtIn.Text
txtOut.Text = "No"
For i As Integer = 0 To lstRoseBowl.Items.Count - 1
If input = CStr(lstBox.Items(i)) Then
txtOut.Text = "Yes"
Exit For
End If
Next
End Sub

Index out of range error when selecting any row other than 1st

I'm getting the "Index out of range error", when double clicking the datagridview any row other than 1st. Double clicking 1st row gives the value. Please help
My code as follows:
Private Sub dgv_searchassistant_CellDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_searchassistant.CellDoubleClick
Dim rowindex As Integer = dgv_searchassistant.CurrentCell.RowIndex
Dim columnindex As Integer = dgv_searchassistant.CurrentCell.ColumnIndex
TextBox30.Text = dgv_searchassistant.Rows(rowindex).Cells(columnindex).Value
TextBox27.Text = dgv_searchassistant.Rows(rowindex).Cells(2).Value
dgv_searchassistant.Visible = False
dgv_searchassistant.AutoGenerateColumns = False
dgv_searchassistant.DataSource = Nothing
End Sub

Datagridview (VB 2010) Readonly property for a column, "strange" behavior

I encountered a strange behavior with the following code on a datagridview in VB2010.
dgv is the name of the datagridview. In FormLoad, I wrote
Private Sub frmdataLog_Load(sender As Object, e As System.EventArgs) Handles Me.Load
dgv.ColumnCount = 5
dgv.Columns(0).Width = 27
Dim j As Integer
For j = 1 To 4
dgv.Columns(j).Width = 85
Next
dgv.Rows.Add(1)
dgv.Columns(0).HeaderText = "Sl No."
dgv.Columns(1).HeaderText = "Test Reading Nm"
dgv.Columns(2).HeaderText = "Standard Reading Nm"
dgv.Columns(3).HeaderText = "Error in Nm"
dgv.Columns(4).HeaderText = "% Uncertainty"
For j = 0 To 5
dgv.Columns(j).ReadOnly = True
dgv.Columns(j).SortMode = DataGridViewColumnSortMode.NotSortable
Next
dgv.Columns(1).ReadOnly = False
End Sub
I am expecting column(1) to be editable while all other columns will be uneditable.
But I found that even column(1) is also readonly!
But when I added the following code, the problem got solved:
Private Sub dgv_CellEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellEnter
colVal = e.ColumnIndex
rowVal = e.RowIndex
If dgv.Columns(1).ReadOnly = True Then
dgv.Columns(1).ReadOnly = False
End If
End Sub
My question is why does the line dgv.Columns(1).ReadOnly = False not execute under Form load?