473,516 Members | 2,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGridView leaves an empty row after escape pressed on new row

Hello,

I have a datagridview bound to a database table with 2 columns. One is an ID column "NameID" which is hidden, the other is called "Name".

The schema picture is here : http://img89.imageshack.us/img89/1251/dbschemaep9.jpg

What happens is :
1. Form loads with values from database inside the DGV
2. I press the checkbox to enable editing/adding etc in the grid (see code event)

Picture : http://img145.imageshack.us/img145/8819/step1yy3.jpg

3good. I click inside the NewRow and type "a" or whatever (something invalid) ...

Picture : http://img80.imageshack.us/img80/1923/step2cl5.jpg

4good. and press escape ... the row is deleted! (Just as I want it)

But... alternatively

3bad. I click inside the NewRow and type "a" or whatever (something invalid), then click outside the DGV on the checkbox or the button, the DGV still keeps focus as "a" fails validation and forces the DGV to keep focus, an error icon is also shown as normal

Picture : http://img80.imageshack.us/img80/1923/step2cl5.jpg
Picture : http://img89.imageshack.us/img89/4729/step3jl5.jpg

4bad. THEN I press escape ... and there is an empty row left! I have no idea why this happens as the focus never left the DGV (as far as I know) so I dont see why the behaviour is different.

Picture : http://img89.imageshack.us/img89/8317/step4yg5.jpg

==================

Please if anyone knows what Im doing wrong, let me know, I havent been able to figure out why it does this for over a day.

==================

CODE :

Imports System.Text.RegularExpressions

Public Class frmNames

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SampleDbDataSet.Names' table. You can move, or remove it, as needed.
Me.NamesTableAdapter.Fill(Me.SampleDbDataSet.Names )
End Sub

Private Sub dgvNames_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvNames.CellEndEdit
' Remove the error icon (valid input or escape pressed)
dgvNames.Rows(e.RowIndex).ErrorText = String.Empty
End Sub

Private Sub dgvNames_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvNames.CellValidated
' Get reference to current datarow
Dim CurrentDataRow As SampleDbDataSet.NamesRow = DirectCast(NamesBindingSource.Current, DataRowView).Row

' Update current row
If CurrentDataRow.RowState <DataRowState.Unchanged Then
NamesTableAdapter.Update(CurrentDataRow)
End If
End Sub

Private Sub dgvNames_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEve ntArgs) Handles dgvNames.CellValidating
' Don't validate an empty row
If dgvNames.Rows(e.RowIndex).IsNewRow Then Exit Sub

' Check the name against a pattern
If Regex.IsMatch(e.FormattedValue, "^[A-Z]\. [A-Z][a-z]+['-]?[a-z]+$") = False Then
dgvNames.Rows(e.RowIndex).ErrorText = "The name is invalid, it should be something like A. Mohammed"
e.Cancel = True
End If
End Sub

Private Sub chkEdit_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEdit.CheckedChanged
' Enable or disable the grid for editing
With dgvNames
.ColumnHeadersVisible = chkEdit.Checked
.RowHeadersVisible = chkEdit.Checked
.AllowUserToAddRows = chkEdit.Checked
.AllowUserToDeleteRows = chkEdit.Checked
.ReadOnly = Not chkEdit.Checked
End With
End Sub

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
' Close the form
Me.Close()
End Sub
End Class

--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-

Jul 26 '06 #1
0 9082

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7373
by: Binny V A | last post by:
Hello Everyone, I am new to python and I am trying to get a program to close a application when the Escape Key is pressed. This is the code that I used --------------------------------- from Tkinter import *
0
1103
by: YiFai | last post by:
Hello, I'm currently writing a directx program with C++, but it takes a while to load the files necessary for the directx. When the directx window loads, it can quit the program with the 'escape' key by using the keydown events provided by directx libraries. What I would like to do is be able to quit the program before it finishes...
16
3320
by: sudhir | last post by:
hi how to check escape key is pressed when accepting the string as input. Because I do not want to receive a string if user presses the ESCAPE key.. I used ascii code for comparision but I didn't get any fruitful results. Please help me.
0
6913
by: TNSFED | last post by:
I have a dilemma when trying to delete a row from the DataGridView. Here is a sample of my code: private void dgv_EQUPS_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { fDeleteRow = false; if (chx_DeleteFlag.Checked) {
1
7769
by: George | last post by:
Hi, Does anyone know how to simulate the 'escape' key functionality for cancelling add ing a new row in DataGridView. I have attempted to use CancelEdit(). They seem to only remove/dispose the editing of the current cell of the added row. However, the new row is still there. One way I can think of is <below>, but I think it is...
0
911
by: Asif Mohammed | last post by:
Hello, I have a datagridview bound to a database table with 2 columns. One is an ID column "NameID" which is hidden, the other is called "Name". The schema picture is here : http://img89.imageshack.us/img89/1251/dbschemaep9.jpg What happens is : 1. Form loads with values from database inside the DGV 2. I press the checkbox to enable...
0
1285
by: Asif Mohammed | last post by:
Hello, I have a datagridview bound to a database table with 2 columns. One is an ID column "NameID" which is hidden, the other is called "Name". The schema picture is here : http://img89.imageshack.us/img89/1251/dbschemaep9.jpg What happens is : 1. Form loads with values from database inside the DGV 2. I press the checkbox to enable...
3
5231
by: hzgt9b | last post by:
Using VS2005, VB.NET, I am developing a windows app that has a DataGridView. I want to enable the display of a context menu on this DataGridView only when a specific set of keys is also pressed (like CTRL+ALT+SHIFT). TO this point I have code that only displays the context menu while keys CTRL +ALT+SHIFT are pressed - but after the keys are...
0
1872
by: jchaturv | last post by:
All, I have a customized DataGridView where I have implemented extra functionality and bounded datasource in the same custom DataGridView (Using C# and .NET). Now, I could able to use it properly by placing it on a panel control. I have added label as button on panel control to display data from datasource on to datagrid and create a...
0
7408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7581
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5714
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3267
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1624
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
825
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.