|
-
Apr 4th, 2011, 01:05 AM
#1
Thread Starter
Addicted Member
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:
Public Class Form2
Dim dt As New DataTable("view")
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dt.Columns.Add(New DataColumn("Col1"))
dt.Columns.Add(New DataColumn("Col2"))
dg.DataMember = "view"
dg.DataSource = dt
End Sub
'new rows
Private Sub btnAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRow.Click
addRow()
End Sub
Sub addRow()
Dim dr As DataRow
dr = dt.NewRow
dr.Item(0) = ""
dr.Item(1) = ""
dt.Rows.Add(dr)
End Sub
Private Sub dg_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dg.KeyUp
If e.KeyData = Keys.Enter Then
Dim cCol = dg.CurrentCell.ColumnIndex
If cCol = 1 Then
addRow()
End If
'End Select
End If
End Sub
End Class
the problem at dg_KeyUp code, Since more than 1 row created while I want only 1 line only.
thank you
-
Apr 4th, 2011, 02:53 AM
#2
Thread Starter
Addicted Member
Re: DatagridView KeyUp add New Row Problem
anyone can help??
-
Apr 4th, 2011, 03:10 AM
#3
Hyperactive Member
Re: DatagridView KeyUp add New Row Problem
If your Datagridview is bound to a Datatable, then try the following code in addrow()
vb Code:
Dim row As DataRow = dt.NewRow row("Col1") = "Name1" row("Col2") = "Name2" dt.Rows.Add(row)
-
Apr 4th, 2011, 03:24 AM
#4
Thread Starter
Addicted Member
Re: DatagridView KeyUp add New Row Problem
thanks for the replay.
what's the difference with my code above
vb Code:
Sub addRow()
Dim dr As DataRow
dr = dt.NewRow
dr.Item(0) = ""
dr.Item(1) = ""
dt.Rows.Add(dr)
End Sub
dr.Item(0) and dr(0) only way of writing it
-
Apr 4th, 2011, 03:44 AM
#5
Hyperactive Member
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 ?
-
Apr 4th, 2011, 03:50 AM
#6
Thread Starter
Addicted Member
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 .
-
Apr 4th, 2011, 04:15 AM
#7
Hyperactive Member
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:
Private Sub dg_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dg.KeyDown If e.KeyCode = Keys.Enter Then addRow() e.Handled = True End If 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|