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.Reg ularExpressions
Public Class frmNames
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
'TODO: This line of code loads data into the 'SampleDbDataSe t.Names' table. You can move, or remove it, as needed.
Me.NamesTableAd apter.Fill(Me.S ampleDbDataSet. Names)
End Sub
Private Sub dgvNames_CellEn dEdit(ByVal sender As Object, ByVal e As System.Windows. Forms.DataGridV iewCellEventArg s) Handles dgvNames.CellEn dEdit
' Remove the error icon (valid input or escape pressed)
dgvNames.Rows(e .RowIndex).Erro rText = String.Empty
End Sub
Private Sub dgvNames_CellVa lidated(ByVal sender As Object, ByVal e As System.Windows. Forms.DataGridV iewCellEventArg s) Handles dgvNames.CellVa lidated
' Get reference to current datarow
Dim CurrentDataRow As SampleDbDataSet .NamesRow = DirectCast(Name sBindingSource. Current, DataRowView).Ro w
' Update current row
If CurrentDataRow. RowState <DataRowState.U nchanged Then
NamesTableAdapt er.Update(Curre ntDataRow)
End If
End Sub
Private Sub dgvNames_CellVa lidating(ByVal sender As Object, ByVal e As System.Windows. Forms.DataGridV iewCellValidati ngEventArgs) Handles dgvNames.CellVa lidating
' Don't validate an empty row
If dgvNames.Rows(e .RowIndex).IsNe wRow 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).Erro rText = "The name is invalid, it should be something like A. Mohammed"
e.Cancel = True
End If
End Sub
Private Sub chkEdit_Checked Changed(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles chkEdit.Checked Changed
' Enable or disable the grid for editing
With dgvNames
.ColumnHeadersV isible = chkEdit.Checked
.RowHeadersVisi ble = chkEdit.Checked
.AllowUserToAdd Rows = chkEdit.Checked
.AllowUserToDel eteRows = chkEdit.Checked
.ReadOnly = Not chkEdit.Checked
End With
End Sub
Private Sub btnClose_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) 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/ =-