הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Saturday, September 23, 2017 12:34 AM
I have a few rows in datagridview, double a cell or press F2 to begin editing the content, the editing will be ended when mouse click other rows. But when click the blank area in datagridview or anywhere outside this datagridview, the editing will not ended. I want to end the editing when click anywhere except the edited cell, how to do? thank you.
All replies (8)
Saturday, September 23, 2017 1:39 PM ✅Answered
Public Class Form2
Private Sub DataGridView1_MouseClick(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseClick
Dim ht = DataGridView1.HitTest(e.X, e.Y)
If ht.Type = DataGridViewHitTestType.None Then
DataGridView1.EndEdit()
End If
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add(New Object() {"Karen", "Payne", #1/1/2017#})
DataGridView1.Rows.Add(New Object() {"Mary", "Gallagher", #7/8/2016#})
DataGridView1.Rows.Add(New Object() {"John", "Brown", #12/4/2014#})
End Sub
End Class
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Saturday, September 23, 2017 12:58 AM
How are you loading the DataGridView as there are many ways ranging from adding rows to the DataGridViewRowCollection, TableAdapter, DataSet, DataTable, List(of T) etc. ?
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Saturday, September 23, 2017 5:58 AM
I added the rows using the dgv.rows.add method. thank you.
Saturday, September 23, 2017 10:01 AM
I have created a DataGridView, add columns via the designer
Added rows
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add(New Object() {"Karen", "Payne", #1/1/2017#})
DataGridView1.Rows.Add(New Object() {"Mary", "Gallagher", #7/8/2016#})
DataGridView1.Rows.Add(New Object() {"John", "Brown", #12/4/2014#})
End Sub
End Class
Now let's add an event for when the mouse leaves the confines of the DataGridView we can check to see if there is dirty data.
Public Class Form2
Private Sub DataGridView1_MouseLeave(sender As Object, e As EventArgs) Handles DataGridView1.MouseLeave
If DataGridView1.IsCurrentCellDirty Then
Console.WriteLine("Yes")
End If
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add(New Object() {"Karen", "Payne", #1/1/2017#})
DataGridView1.Rows.Add(New Object() {"Mary", "Gallagher", #7/8/2016#})
DataGridView1.Rows.Add(New Object() {"John", "Brown", #12/4/2014#})
End Sub
End Class
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Saturday, September 23, 2017 12:18 PM
Sorry, double-click the cell, when mouse click the blank area or outside datagridview1, the cell can not end edit status. thank you.
I wish the action like Windows Explorer, for example rename the file, click the file name twice, after rename, click other blank area, the rename action will be ended.
Saturday, September 23, 2017 12:28 PM
Then edit it yourself e.g.
Public Class Form2
Private Sub DataGridView1_MouseLeave(sender As Object, e As EventArgs) Handles DataGridView1.MouseLeave
If DataGridView1.IsCurrentCellDirty Then
DataGridView1.EndEdit()
End If
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add(New Object() {"Karen", "Payne", #1/1/2017#})
DataGridView1.Rows.Add(New Object() {"Mary", "Gallagher", #7/8/2016#})
DataGridView1.Rows.Add(New Object() {"John", "Brown", #12/4/2014#})
End Sub
End Class
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Saturday, September 23, 2017 1:30 PM
Sorry, i have tried it, the MouseLeave and CellMouseLeave occur when the mouse pointer leaves the control and cell, i wish CLICK the blank region in datagirdview or the region outside datagridview to end the edit mode.
Saturday, September 23, 2017 10:31 PM
Wow, yes, it works perfectly, thank you very much.