Results 1 to 7 of 7

Thread: DatagridView KeyUp add New Row Problem

  1. #1

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    DatagridView KeyUp add New Row Problem

    Hi,
    i have problem when addnew row at keyup event for datagridview control.
    i want to add one row when method addRow() called.

    this my code :
    vb.net Code:
    1. Public Class Form2
    2.     Dim dt As New DataTable("view")
    3.  
    4.     Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    5.         dt.Columns.Add(New DataColumn("Col1"))
    6.         dt.Columns.Add(New DataColumn("Col2"))
    7.         dg.DataMember = "view"
    8.         dg.DataSource = dt
    9.     End Sub
    10.  
    11.     'new rows
    12.     Private Sub btnAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRow.Click
    13.         addRow()
    14.     End Sub
    15.     Sub addRow()
    16.         Dim dr As DataRow
    17.         dr = dt.NewRow
    18.         dr.Item(0) = ""
    19.         dr.Item(1) = ""
    20.         dt.Rows.Add(dr)
    21.     End Sub
    22.  
    23.     Private Sub dg_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dg.KeyUp
    24.         If e.KeyData = Keys.Enter Then
    25.             Dim cCol = dg.CurrentCell.ColumnIndex
    26.             If cCol = 1 Then
    27.                 addRow()
    28.             End If
    29.             'End Select
    30.         End If
    31.     End Sub
    32. End Class

    the problem at dg_KeyUp code, Since more than 1 row created while I want only 1 line only.

    thank you

  2. #2

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: DatagridView KeyUp add New Row Problem

    anyone can help??

  3. #3
    Hyperactive Member Ram2Curious's Avatar
    Join Date
    Apr 2010
    Posts
    484

    Re: DatagridView KeyUp add New Row Problem

    If your Datagridview is bound to a Datatable, then try the following code in addrow()

    vb Code:
    1. Dim row As DataRow = dt.NewRow
    2. row("Col1") = "Name1"
    3. row("Col2") = "Name2"
    4. dt.Rows.Add(row)

  4. #4

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: DatagridView KeyUp add New Row Problem

    thanks for the replay.
    what's the difference with my code above
    vb Code:
    1. Sub addRow()
    2.         Dim dr As DataRow
    3.         dr = dt.NewRow
    4.         dr.Item(0) = ""
    5.         dr.Item(1) = ""
    6.         dt.Rows.Add(dr)
    7.     End Sub

    dr.Item(0) and dr(0) only way of writing it

  5. #5
    Hyperactive Member Ram2Curious's Avatar
    Join Date
    Apr 2010
    Posts
    484

    Re: DatagridView KeyUp add New Row Problem

    What exactly do you want to achieve through the KeyUp event ? It would be helpful if you spot it out more clearly. Do you want to generate new rows dynamically as you keep on hitting the Enter key ?

  6. #6

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: DatagridView KeyUp add New Row Problem

    when I press the enter key while in column 1 on the grid I would like for a line just added.
    but what happened instead 2 rows straight.

    and if I press button btnAddRow can work well rather than using the enter key .

  7. #7
    Hyperactive Member Ram2Curious's Avatar
    Join Date
    Apr 2010
    Posts
    484

    Re: DatagridView KeyUp add New Row Problem

    At the moment i was able to come up with this. This code can generate single rows when you hit the Enter key.

    Try this :

    vb Code:
    1. Private Sub dg_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dg.KeyDown
    2.  
    3.         If e.KeyCode = Keys.Enter Then
    4.  
    5.             addRow()
    6.  
    7.             e.Handled = True
    8.  
    9.         End If
    10.  
    11. End Sub
    Last edited by Ram2Curious; Apr 4th, 2011 at 06:40 AM. Reason: Code Correction.

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