PC Review


Reply
Thread Tools Rate Thread

DataGridView leaves an empty row after escape pressed on new row

 
 
Asif Mohammed
Guest
Posts: n/a
 
      26th Jul 2006
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.DataGridViewCellValidatingEventArgs) 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/ =-

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reposting : DataGridView leaves an empty row after escape pressed onnew row Asif Mohammed Microsoft VB .NET 0 28th Jul 2006 08:15 AM
How to disable undo when the escape key is pressed? =?Utf-8?B?TGl6IEphbWVz?= Microsoft Access 5 18th Jan 2005 07:46 AM
Escape sequence to dotmatrix printer - Escape() API Funbeat Microsoft Dot NET Framework 1 3rd Aug 2004 11:24 AM
Javascript escape & JScript GlobalObject.escape Hugo Wetterberg Microsoft ASP .NET 1 15th Mar 2004 07:48 PM
How can I GetFocus for a specific field after escape key is pressed? John Bildy Microsoft Access Forms 3 14th Nov 2003 05:19 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:30 AM.