Dear Developers

SQL, Table1 has data as following

Code------product
1-----------Mango
2-----------Potato
3-----------Orange

Datagridview has THREE columns as

Code----product-------qty

I have following codes

str3 = "SELECT product FROM table where code =" & Val(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value)
        dt3 = GetTable3(str3)

        If dt3.Rows.Count > 0 Then
            DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value = (IIf(IsDBNull(dt3.Rows(0).Item("product")), "", dt3.Rows(0).Item("product")))
                    Else
            MsgBox("Code Not Found", MsgBoxStyle.Information)
        End If

If code not found then msgbox appears and cursor moves to NEXT COLUMN. That works fine

But I want ......

When code not found then cursor MUST NOT GO TO NEXT COLUMN. It should remain there until it found value from Table1.

Should I change code or choose some other datagridview event?

Please help

>Should I change code or choose some other datagridview event?

Yes. Think about CellValidating/CellValidated event.

sir, i have tested my codes while using said events but cusor moves to next column in every case.

I want to stop cursor in columnindex=0 if no value is found from database

Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        Dim currCell As DataGridViewCell = DataGridView1.CurrentCell
        If currCell.Value.ToString().Length = 0 Then
            e.Handled = True
            DataGridView1.CurrentCell = DataGridView1.Item(currCell.ColumnIndex, currCell.RowIndex)
        End If
    End Sub
Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        Dim currCell As DataGridViewCell = DataGridView1.CurrentCell
        If currCell.Value.ToString().Length = 0 Then
            e.Handled = True
            DataGridView1.CurrentCell = DataGridView1.Item(currCell.ColumnIndex, currCell.RowIndex)
        End If
    End Sub

When columnindex=0 is 0 then
it has following error

Attachments grid.JPG 94.44 KB
IF Not IsNothing(currCell) Then
  If currCell.Value.ToString().Length = 0 Then
            e.Handled = True
            DataGridView1.CurrentCell = DataGridView1.Item(currCell.ColumnIndex, currCell.RowIndex)
        End If
End If
IF Not IsNothing(currCell) Then
  If currCell.Value.ToString().Length = 0 Then
            e.Handled = True
            DataGridView1.CurrentCell = DataGridView1.Item(currCell.ColumnIndex, currCell.RowIndex)
        End If
End If

on this line
If currCell.Value.ToString().Length = 0 Then

it says
Object reference not set to an instance of an object.

MR SAMIR IBRAHIM FROM LEBANON Has given the correct answer as

Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        If Me.DataGridView1.CurrentCell.ColumnIndex = 0 Then
            If Len(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value) = 7 Then

                str = "SELECT desc1,weight FROM master where code =" & Val(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value)
                dt = GetTable(str)
                Dim myCell As DataGridViewCell = DataGridView1.CurrentCell

                If dt.Rows.Count > 0 Then
                    DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value = (IIf(IsDBNull(dt.Rows(0).Item("desc1")), "", dt.Rows(0).Item("desc1")))
                    DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value = (IIf(IsDBNull(dt.Rows(0).Item("weight")), "0", dt.Rows(0).Item("weight")))
                Else
                    MsgBox("Code Not Found", MsgBoxStyle.Information)
                    SendKeys.Send("{left}")
                    ' SendKeys.Send("{UP}")
                    DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value = Nothing
                End If

            Else
                MsgBox("Wrong Code", MsgBoxStyle.Information)
                SendKeys.Send("{left}")
                DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value = Nothing
            End If
        End If

    End Sub

So all credite goes to him

Edited 6 Years Ago by vbdotnetlover: n/a

This question has already been answered. Start a new discussion instead.