CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2008
    Posts
    70

    DATAGRIDVIEW for inventory items entry

    Hi

    My datagraidview (vb 2005) contains 5 columns -

    1. in the first column I want to type some code, then press enter key, upon press enter key
    a listview (any poppup window) should appear, user can select an item from the list,
    then the selected item's details should appear in other columns.

    2. how can we trap the key press event of the cell in the datagridview?


    thanks
    VB.Net 2005, Net Framework 2.0

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: DATAGRIDVIEW for inventory items entry

    Okay you will need to setup a boolean variable for this ...
    Code:
    Private CellEdit as Boolean
    In the DGV.CellBeginEdit event you check what cell is been edited and set the Var 'True' and in the DGV.CellEndEdit you set it 'False'..

    Now in the DGV.KeyPress/KeyDown/KeyUp you can check if the CellEdit var is set and trap the keys you need..

    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Aug 2008
    Posts
    70

    Re: DATAGRIDVIEW for inventory items entry

    following is the code, but not working as intended,
    I cannot get the keypress event of datagridview, I extended a method, that maoves the cursor to the right, ok that working.

    But intended purpose does'nt serve this




    Public Class itemMaster

    Private CellEdit As Boolean, rowiNdex, columniNdex As Int16
    Dim WithEvents dgv As New exDataGridView

    Private Sub itemMaster_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    dgv.Width = 500 : dgv.Height = 200
    dgv.Location = New Point(40, 300)
    dgv.ColumnCount = 5
    dgv.Columns(0).Name = "Sl No"
    dgv.Columns(1).Name = "Category"
    dgv.Columns(2).Name = "Quantity"
    dgv.Columns(3).Name = "Rate"
    dgv.Columns(4).Name = "Amount"


    AddHandler dgv.CellBeginEdit, AddressOf dg_CellBeginEdit
    AddHandler dgv.CellEndEdit, AddressOf dg_CellEndEdit

    Me.Controls.Add(dgv)
    formLoaded = True
    End Sub

    Private Sub dg_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs)
    rowiNdex = e.RowIndex : columniNdex = e.ColumnIndex
    CellEdit = True
    End Sub

    Private Sub dg_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
    CellEdit = False
    End Sub


    Private Sub dg_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    If CellEdit Then
    MessageBox.Show(dgv.Item(columnNdex, rowNdex).Value)
    End If
    End If
    End Sub

    end class
    Public Class ExDataGridView
    Inherits DataGridView
    Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
    Dim key As Keys = keyData & Keys.KeyCode
    If (key = Keys.Enter) Then
    Return Me.ProcessRightKey(keyData)
    Else
    Return MyBase.ProcessDialogKey(keyData)
    End If
    End Function
    Protected Overrides Function ProcessDataGridViewKey(ByVal e As KeyEventArgs) As Boolean
    If (e.KeyCode = Keys.Enter) Then
    Return Me.ProcessRightKey(e.KeyData)
    Else
    Return MyBase.ProcessDataGridViewKey(e)
    End If
    End Function
    End Class
    VB.Net 2005, Net Framework 2.0

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: DATAGRIDVIEW for inventory items entry

    Please Use CODE TAGS [code] Your code here [/code] to post code....

    Try something along this line ...
    Code:
        Private CellEdit As Boolean
    
        Private Sub DGV1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DGV1.CellBeginEdit
            If e.ColumnIndex = 0 Then CellEdit = True
        End Sub
    
     
        Private Sub DGV1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV1.CellEndEdit
            CellEdit = False
        End Sub
    
        Private Sub DGV1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV1.CellValueChanged
            If CellEdit Then
                MsgBox("First Cell Edit Completed.")
                'Show your List here...
            End If
        End Sub
    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    Aug 2008
    Posts
    70

    Re: DATAGRIDVIEW for inventory items entry

    no, no
    I want to show the listview (any popup window) only when enter key is pressed.

    how to get the enter key in this context?
    VB.Net 2005, Net Framework 2.0

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: DATAGRIDVIEW for inventory items entry

    Well I tried to push you in the right direction.... I gave you just about everything you needed to do this, but allas, you need someone to do the code for you.... How are you ever going to learn to do these things...

    IF you had to follow my post and added it to what you already had ... you may have come out with something along the lines of ..

    EXDGVClass..
    Code:
     Public Class ExDataGridView
        Inherits DataGridView
        Public Event CellKeyPress(ByVal KeyData As Keys)
    
        Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
            RaiseEvent CellKeyPress(keyData)
        End Function
    End Class
    Quick, Clean, Easy..... NO MESS ........


    Then in your form Designer... you use...
    Code:
        Friend WithEvents dgv1 As ExDataGridView
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Me.dgv1 = New ExDataGridView
            CType(Me.dgv1, System.ComponentModel.ISupportInitialize).BeginInit()
            '
            'dgv1
            '
            Me.dgv1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
            Me.dgv1.Location = New System.Drawing.Point(13, 4)
            Me.dgv1.Name = "dgv1"
            Me.dgv1.Size = New System.Drawing.Size(240, 150)
            Me.dgv1.TabIndex = 0
            Me.Controls.Add(Me.dgv1)
            CType(Me.dgv1, System.ComponentModel.ISupportInitialize).EndInit()
    
    ......
    Most Of this is autogenerated, but it's worth the while to dbl check it...

    Then to Get EXACTLY what you looking for .... you use...
    Code:
        Private CellEdit As Boolean
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            dgv1.ColumnCount = 5
            dgv1.Columns(0).Name = "Sl No"
            dgv1.Columns(1).Name = "Category"
            dgv1.Columns(2).Name = "Quantity"
            dgv1.Columns(3).Name = "Rate"
            dgv1.Columns(4).Name = "Amount"
    
        End Sub
    
    
        Private Sub DGV1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgv1.CellBeginEdit
            If e.ColumnIndex = 0 Then CellEdit = True
        End Sub
    
    
        Private Sub DGV1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv1.CellEndEdit
            CellEdit = False
        End Sub
    
        Private Sub dgv1_CellKeyPress(ByVal KeyData As System.Windows.Forms.Keys) Handles dgv1.CellKeyPress
            If CellEdit Then
                If KeyData = Keys.Enter Then
                    MsgBox("SHOW YOUR LIST HERE")
                End If
            End If
        End Sub
    .... again ... Quick, Clean, Easy ........


    I've tested this and it will show the msgbox ONLY WHEN enter is pressed in the first Column (any row) during a cell Edit.......
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  7. #7
    Join Date
    Aug 2008
    Posts
    70

    Re: DATAGRIDVIEW for inventory items entry

    Why I not getting the cell value at cellkeypress - see the code below

    Private Sub dgv1_CellKeyPress(ByVal KeyData As System.Windows.Forms.Keys) Handles dgv1.CellKeyPress
    If CellEdit Then
    If KeyData = Keys.Enter Then
    MsgBox(columniNdex & ":" & rowiNdex)
    MessageBox.Show(dgv1.Item(columniNdex, rowiNdex).Value)
    End If
    End If
    End Sub
    VB.Net 2005, Net Framework 2.0

  8. #8
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: DATAGRIDVIEW for inventory items entry

    Simple .... The Cell value has not been processed yet, IE .. Is still Previous Value. WHY ??

    Well we trapping the enter key before the DGV has time to process the key.. so the Cell Value is still what it was before the edit (Empty) and what the user has typed is in a buffer .. (this allows a user to 'Cancel' Edits) ......

    So IF you want to catch the new Value, you have to check for it in 'CellEndEdit' sub.Simply just set a new Boolean value when enter is pressed and check for it here.. DONT FORGET to clear it after your done processing it...

    Gremmy....
    Last edited by GremlinSA; January 6th, 2009 at 05:56 AM.
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  9. #9
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: DATAGRIDVIEW for inventory items entry

    Ahh .. what the hell....

    HERE ... put this code in your form...
    Code:
    Public Class Form1
        Private CellEdit As Boolean
        Private EnterPessed As Boolean
        Private columniNdex As Integer
        Private rowiNdex As Integer
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
            Dgv1.ColumnCount = 5
            Dgv1.Columns(0).Name = "Sl No"
            Dgv1.Columns(1).Name = "Category"
            Dgv1.Columns(2).Name = "Quantity"
            Dgv1.Columns(3).Name = "Rate"
            Dgv1.Columns(4).Name = "Amount"
    
        End Sub
    
    
        Private Sub DGV1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles Dgv1.CellBeginEdit
            If e.ColumnIndex = 0 Then CellEdit = True
            rowiNdex = e.RowIndex
            columniNdex = e.ColumnIndex
        End Sub
    
    
        Private Sub DGV1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Dgv1.CellEndEdit
            CellEdit = False
            If EnterPessed Then
                MsgBox(columniNdex & ":" & rowiNdex)
                MessageBox.Show(Dgv1.Item(columniNdex, rowiNdex).Value)
                EnterPessed = False
            End If
        End Sub
    
        Private Sub dgv1_CellKeyPress(ByVal KeyData As System.Windows.Forms.Keys) Handles Dgv1.CellKeyPress
            If CellEdit Then
                If KeyData = Keys.Enter Then
                    EnterPessed = True
                End If
            End If
        End Sub
    End Class
    AND PLEASE use code tags when posting code ..... i did show you how to in post #4 ....

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  10. #10
    Join Date
    Aug 2008
    Posts
    70

    Re: DATAGRIDVIEW for inventory items entry

    Thanks up to this point, ok.
    the following are my other requirements

    1. When I press small case letter, it should be converted to Uppercase
    2. I want to able to enter only ALPHABETS & ARABIC NUMERALS ONLY (+ ENTER KEY)
    3. can I get the position of cell (point in x,y cordinates) relative to the screen or grid

    thanks in advance
    VB.Net 2005, Net Framework 2.0

  11. #11
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: DATAGRIDVIEW for inventory items entry

    Quote Originally Posted by marinajogy View Post
    Thanks up to this point, ok.
    the following are my other requirements

    1. When I press small case letter, it should be converted to Uppercase
    2. I want to able to enter only ALPHABETS & ARABIC NUMERALS ONLY (+ ENTER KEY)
    3. can I get the position of cell (point in x,y cordinates) relative to the screen or grid

    thanks in advance
    Hmm .... dont think you going to get it that easy ... ALL THREE of these topics have been discussed in length on the forum ... do a little searching ....

    Hints ..

    Ucase, Filter Keys, Position, Coordinate...

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured