Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2003
    Messaggi
    5

    DatagridView e tasto Enter

    Ciao, Ragazzi...

    Sono nuovo del VB NET pertanto chiedo scusa in anticipo se scriverò qualche bestialità.

    Dunqu ho un problema:

    Ho una classe (DatagridView) che gestisce la pressione dle tasto Enter come se fosse il tasto 'Freccia Destra' e fin qui tutto ok.

    quando inserisco il codice che corrisponde ad un fornitore, una query mi riempie la colonna 2 con il nominativo di quel fornitore e sposta il focus nella cella numero quattro.

    Ora io vorrei che qualore per errore digitassi un codice inesistente, il focus noj si spostasse...

    Mi hanno suggerito di usare HasRows, e così ho fatto, solo che non mi spiego perché La condizioen HasRows rimane sempre su 'False' e pertanto il focus viene spostato a prescindere dal risultato della query...

    Questo è il codice, spero possiate aiutarmi:

    Imports System.Data.OleDb
    Imports System.Windows.Forms

    Public Class DGVMod
    Inherits DataGridView



    Protected Overrides Sub OnCellValidated(ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

    Try
    rw = CurrentCell.RowIndex
    cl = CurrentCell.ColumnIndex

    Select Case cl

    Case Is = 1

    Dim cmd As New OleDb.OleDbCommand("SELECT FORNITORE FROM FORNITORI WHERE ID_FOR = '" & Item(1, rw).Value & "'", cn)
    dr = cmd.ExecuteReader
    If dr.HasRows And Item(11, rw).Value = 0 Then

    Do While dr.Read
    Item(2, rw).Value = dr.Item("FORNITORE")
    Loop
    dr.Close()

    ElseIf dr.HasRows And Item(11, rw).Value > 0 Then
    Do While dr.Read
    Item(2, rw).Value = dr.Item("FORNITORE")
    Loop
    cmd = New OleDb.OleDbCommand("UPDATE VENDITE SET ID_FOR = " & Item(1, rw).Value & ", FORNITORE = '" & Item(2, rw).Value & "' WHERE ID_VEN = " & Item(11, rw).Value, cn)
    dr = cmd.ExecuteReader
    dr.Close()
    Else
    MsgBox("CVCB")
    dr.Close()
    End If

    Case Is = 3

    sql = "select ARTICOLO, ALIQUOTA from ARTICOLI where ID_ART=?"
    Dim cmd As New OleDb.OleDbCommand(sql, cn)
    cmd.Parameters.AddWithValue("ID_ART", Item(3, rw).Value)
    dr = cmd.ExecuteReader

    If dr.HasRows And Item(11, rw).Value = 0 Then
    Do While dr.Read
    Item(4, rw).Value = dr.Item("ARTICOLO")
    Item(12, rw).Value = dr.Item("ALIQUOTA")
    Loop
    dr.Close()

    ElseIf dr.HasRows And Item(11, rw).Value > 0 Then
    Do While dr.Read
    Item(4, rw).Value = dr.Item("ARTICOLO")
    Loop
    cmd = New OleDb.OleDbCommand("UPDATE VENDITE SET ID_AR = " & Item(3, rw).Value & ", ARTICOLO = '" & Item(4, rw).Value & "' WHERE ID_VEN = " & Item(11, rw).Value, cn)
    dr = cmd.ExecuteReader
    dr.Close()
    Else
    MsgBox("CVCB")
    dr.Close()
    End If

    Case Is = 5
    If Item(11, rw).Value > 0 Then
    Dim cmd As New OleDb.OleDbCommand("UPDATE VENDITE SET COLLI = " & Item(5, rw).Value & " WHERE ID_VEN = " & Item(11, rw).Value, cn)
    dr = cmd.ExecuteReader
    dr.Close()
    End If

    Case Is = 6

    If Item(12, rw).Value > 0 Then
    Item(12, rw).Value = Val(Item(12, rw).Value) + (Val(Form1.Iva_Text.Text) - Val(Item(12, rw).Value))
    End If
    Item(8, rw).Value = (Val(Item(6, rw).Value) * Val(Item(7, rw).Value)) + _
    (Val(Item(6, rw).Value) * Val(Item(7, rw).Value)) / 100 * Item(12, rw).Value
    If Item(11, rw).Value > 0 Then
    Dim cmd As New OleDb.OleDbCommand("UPDATE VENDITE SET PESO = " & Item(6, rw).Value & ", PREZZO = " & Item(7, rw).Value & ", SUBTOTALE = " & Item(8, rw).Value & " WHERE ID_VEN = " & Item(11, rw).Value, cn)
    dr = cmd.ExecuteReader
    dr.Close()
    End If

    Case Is = 7
    'Dim aliq As Double
    If Item(12, rw).Value > 0 Then
    Item(12, rw).Value = Val(Item(12, rw).Value) + (Val(Form1.Iva_Text.Text) - Val(Item(12, rw).Value))
    End If
    Item(8, rw).Value = (Val(Item(6, rw).Value) * Val(Item(7, rw).Value)) + _
    (Val(Item(6, rw).Value) * Val(Item(7, rw).Value)) / 100 * Item(12, rw).Value
    If Item(11, rw).Value > 0 Then
    Dim cmd As New OleDb.OleDbCommand("UPDATE VENDITE SET PESO = " & Item(6, rw).Value & ", PREZZO = " & Item(7, rw).Value & ", SUBTOTALE = " & Item(8, rw).Value & " WHERE ID_VEN = " & Item(11, rw).Value, cn)
    dr = cmd.ExecuteReader
    dr.Close()
    End If

    Case Is = 9

    sql = "select CLIENTE from CLIENTI where ID_CLI=?"
    Dim cmd As New OleDb.OleDbCommand(sql, cn)
    cmd.Parameters.AddWithValue("ID_CLI", Item(9, rw).Value)
    dr = cmd.ExecuteReader

    If dr.HasRows Then
    Do While dr.Read
    Item(10, rw).Value = dr.Item("CLIENTE")
    Loop
    dr.Close()
    Else
    Item(10, rw).Value = ""
    MsgBox("qui ci scriverò qualcosa")
    dr.Close()
    End If

    If Item(11, rw).Value > 0 Then
    cmd = New OleDb.OleDbCommand("UPDATE VENDITE SET ID_CLI = " & Item(9, rw).Value & ", CLIENTE = '" & Item(10, rw).Value & "' WHERE ID_VEN = " & Item(11, rw).Value, cn)
    dr = cmd.ExecuteReader
    dr.Close()
    Else

    Dim objCmd As OleDb.OleDbCommand
    objCmd = New OleDb.OleDbCommand("Insert into VENDITE (DATA, ID_FOR, FORNITORE, ID_AR, ARTICOLO, COLLI ,PESO, PREZZO, SUBTOTALE, ID_CLI, CLIENTE, ALIQUOTA) Values (" & "'" & Item(0, rw).Value & "'" _
    & ",'" & Item(1, rw).Value & "'" _
    & ",'" & Item(2, rw).Value & "'" _
    & ",'" & Item(3, rw).Value & "'" _
    & ",'" & Item(4, rw).Value & "'" _
    & ",'" & Item(5, rw).Value & "'" _
    & ",'" & Item(6, rw).Value & "'" _
    & ",'" & Item(7, rw).Value & "'" _
    & ",'" & Item(8, rw).Value & "'" _
    & ",'" & Item(9, rw).Value & "'" _
    & ",'" & Item(10, rw).Value & "'" _
    & ",'" & Item(12, rw).Value & "'" & ")", cn)

    Dim LastID As Integer
    objCmd.ExecuteNonQuery()

    objCmd = New OleDb.OleDbCommand("SELECT @@IDENTITY", cn)

    LastID = objCmd.ExecuteScalar()
    Item(11, rw).Value = LastID


    End If

    End Select
    Catch
    MsgBox("Alcuni campi sono stati lasciati vuoti. Si prega di riempirli")
    CurrentRow.DefaultCellStyle.BackColor = Color.Yellow
    Finally
    Item(0, rw).Value = Format(Now, "dd/MM/yyyy")

    End Try


    MyBase.OnCellValidated(e)

    End Sub
    Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
    If e.KeyCode = Keys.F4 Then
    If Item(11, rw).Value > 0 Then
    CurrentRow.Selected = True
    Dim del As Integer = MsgBox("la riga n° " & CurrentCell.RowIndex + 1 & " verrà eliminata, continuare?", vbYesNo + vbExclamation + vbDefaultButton2, "Squarcio")
    If del = DialogResult.Yes Then
    Dim cmd As New OleDb.OleDbCommand("DELETE FROM VENDITE WHERE ID_VEN = " & Item(11, rw).Value, cn)
    dr = cmd.ExecuteReader
    dr.Close()
    Rows.Remove(SelectedRows(0))
    Else
    CurrentRow.Selected = False
    End If
    End If
    End If

    Select Case cl
    Case Is = 1
    If Item(2, rw).Value = "" Then
    CurrentCell = Item(1, rw)
    End If
    End Select


    MyBase.OnKeyUp(e)
    End Sub
    <System.Security.Permissions.UIPermission( _
    System.Security.Permissions.SecurityAction.LinkDem and, _
    Window:=System.Security.Permissions.UIPermissionWi ndow.AllWindows)> _
    Protected Overrides Function ProcessDialogKey( _
    ByVal keyData As Keys) As Boolean



    Dim key As Keys = keyData And Keys.KeyCode
    rw = CurrentCell.RowIndex
    cl = CurrentCell.ColumnIndex

    If key = Keys.Enter Then

    Try
    Select Case cl
    Case Is = 0
    Return Me.ProcessRightKey(keyData)

    Case Is = 1

    Dim cmd As New OleDb.OleDbCommand("SELECT FORNITORE FROM FORNITORI WHERE ID_FOR = '" & Item(1, rw).Value & "'", cn) '& Griglia_Vendite.Item(11, rw).Value, cn)

    dr = cmd.ExecuteReader
    If dr.HasRows Then
    CurrentCell = Item(2, rw)
    Return Me.ProcessRightKey(keyData)
    dr.Close()
    Else
    CurrentCell = Item(0, rw)
    Return Me.ProcessRightKey(keyData)
    dr.Close()
    End If





    Return MyBase.ProcessDialogKey(keyData)



    End Function

    <System.Security.Permissions.SecurityPermission( _
    System.Security.Permissions.SecurityAction.LinkDem and, Flags:= _
    System.Security.Permissions.SecurityPermissionFlag .UnmanagedCode)> _
    Protected Overrides Function ProcessDataGridViewKey( _
    ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean

    cl = CurrentCell.ColumnIndex
    rw = CurrentCell.RowIndex



    If e.KeyCode = Keys.Enter Then
    Try
    Select Case cl
    Case Is = 0
    Return Me.ProcessRightKey(e.KeyData)

    Case Is = 1

    dr = cmd.ExecuteReader
    If dr.HasRows Then
    CurrentCell = Item(2, rw)
    Return Me.ProcessRightKey(e.KeyData)
    dr.Close()
    Else
    CurrentCell = Item(0, rw)
    Return Me.ProcessRightKey(e.KeyData)
    dr.Close()
    End If


    Case Is = 9
    If Rows.Count - 1 = rw Then
    Rows.Add()
    CurrentCell = Item(0, rw + 1)
    Return Me.ProcessRightKey(e.KeyData)

    Else
    CurrentCell = Item(0, rw + 1)
    Return Me.ProcessRightKey(e.KeyData)
    End If
    Dim u As Integer
    Form1.Tot_Text.Text = 0
    For u = 0 To Rows.Count - 1

    Form1.Tot_Text.Text = Val(Item(8, u).Value) + Val(Form1.Tot_Text.Text)

    Next
    End Select
    Catch
    End Try
    End If

    Return MyBase.ProcessDataGridViewKey(e)


    End Function
    End Class

  2. #2
    HasRows non serve ad altro che informarti del fatto che il Reader ha ancora righe di dati da leggere. E' quindi logico che la funzione andrà messa all' interno del Ciclo While e non prima, visto che prima il Reader è ancora chiuso ...

  3. #3
    Utente di HTML.it
    Registrato dal
    Mar 2003
    Messaggi
    5
    Originariamente inviato da raffaeu
    HasRows non serve ad altro che informarti del fatto che il Reader ha ancora righe di dati da leggere. E' quindi logico che la funzione andrà messa all' interno del Ciclo While e non prima, visto che prima il Reader è ancora chiuso ...
    Ciao, Raffaeu e grazie della risposta....

    Io ancora non ne vengo a capo, dunque:

    Dim cmd As New OleDb.OleDbCommand("SELECT FORNITORE FROM FORNITORI WHERE ID_FOR = '" & Item(1, rw).Value & "'", cn)
    dr = cmd.ExecuteReader

    Do While dr.Read
    If dr.HasRows Then
    Item(2, rw).Value = dr.Item("FORNITORE")
    CurrentCell = Item(2, rw)
    Return Me.ProcessRightKey(keyData)
    dr.Close()
    End If
    Loop

    Questo codice dovrebbe impedire che lo stato attivo si sposti sulla cella di indice 2 qualora non esistano record?
    Ho inderito la condizione hasrows all'interno del ciclo do while.. loop, ma non si comporta come dovrebbe.

    Grazie

  4. #4
    Metti Debug qui
    Item(2, rw).Value = dr.Item("FORNITORE")
    e vedi se ci arriva, altrimenti vuol dire che la SELECT (magari) è vuota ...

  5. #5
    Utente di HTML.it
    Registrato dal
    Mar 2003
    Messaggi
    5
    Originariamente inviato da raffaeu
    Metti Debug qui
    Item(2, rw).Value = dr.Item("FORNITORE")
    e vedi se ci arriva, altrimenti vuol dire che la SELECT (magari) è vuota ...
    Sì ci arriva, ma al secondo invio, però... mi spiego:
    Digito '7' nella cella numero 1, premo il tasto Invio e non succede niente, ritorno alla cella numero 1, lascio inavriato il codice ('7') e mi compare nella cella 3 il valore LAMITECK e lo stato attivo passsa alla cella 4. POi se torno nuovamente alla cella 1, ci scrivo un altro codice ('20') lo stato attivo passa alla cella 4, ma il valora nella cella 3 rimane 'LAMITECK', ritorno ancora, digito enter e mi inserisce il valore ANDREA NELLA CELLA 4 ...etc, etc...

    In sostanza è come se l'evento 'pressione del tasto enter' intervenisse alla fine del ciclo impedendo di mandare in esecuzione la query.

    Grazie

  6. #6
    alla fine di ogni case si mette in VB.NET un end select o exit o qualcosa di simile, ...
    In C# si usa il Breck

  7. #7
    Utente di HTML.it
    Registrato dal
    Mar 2003
    Messaggi
    5
    Originariamente inviato da raffaeu
    alla fine di ogni case si mette in VB.NET un end select o exit o qualcosa di simile, ...
    In C# si usa il Breck
    Sì, si chiude con 'End Select'... ma che c'entra col problema che ho esposto?


    Diciamo che il codice è questo...

    cl = CurrentCell.ColumnIndex

    Select Case cl

    Case Is = 1
    Dim cmd As New OleDb.OleDbCommand("SELECT FORNITORE FROM FORNITORI WHERE ID_FOR = '" & Item(1, rw).Value & "'", cn)
    dr = cmd.ExecuteReader

    Do While dr.Read
    If dr.HasRows Then
    Item(2, rw).Value = dr.Item("FORNITORE")
    CurrentCell = Item(2, rw)
    Return Me.ProcessRightKey(keyData)
    dr.Close()
    End If
    Loop

    End Select

  8. #8
    Utente di HTML.it
    Registrato dal
    Mar 2003
    Messaggi
    5
    Lo stesso codice, se posto all'interno della sub OnCellValidated funziona egregiamente, ma il mio problema rimane far sì che lo stato attivo passi ad un'altra qualora la condizione hasrows fosse giusta e per far cio' devo necessariamente gestire la pressione del tasto enter...
    ma come dicevo prima, sembra che hasrows restituisca al primo invio sempre e comunque false.

    ecco lo stesso codice, ma posto 'Sub OncellValidated'

    sql = "select ARTICOLO, ALIQUOTA from ARTICOLI where ID_ART=?"
    Dim cmd As New OleDb.OleDbCommand(sql, cn)
    cmd.Parameters.AddWithValue("ID_ART", Item(3, rw).Value)
    dr = cmd.ExecuteReader

    If dr.HasRows And Item(11, rw).Value = 0 Then
    Do While dr.Read
    Item(4, rw).Value = dr.Item("ARTICOLO")
    Item(12, rw).Value = dr.Item("ALIQUOTA")
    Loop
    dr.Close()

    ElseIf dr.HasRows And Item(11, rw).Value > 0 Then
    Do While dr.Read
    Item(4, rw).Value = dr.Item("ARTICOLO")
    Loop
    cmd = New OleDb.OleDbCommand("UPDATE VENDITE SET ID_AR = " & Item(3, rw).Value & ", ARTICOLO = '" & Item(4, rw).Value & "' WHERE ID_VEN = " & Item(11, rw).Value, cn)
    dr = cmd.ExecuteReader
    dr.Close()
    Else
    MsgBox("CVCB")
    dr.Close()
    End If

    Grazie.

    Ciao

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.