In that link, there is a lot of content.
Search for: "'This seems to work :-)"
Use that solution by RonaldBiemand for maximum flexibility.
Main Topics
Browse All TopicsI wish to use data validation with a datagrid on a windows form, and when the data is invalid, throw up a message and select/set focus to the original cell in the datagrid in which an invalid entry was enterred. The datagrid is on a windows form and I am using .NET Framework 1.1
So far I have done the following. As a test I don't allow for "aa" in a column called "Title"
AddHandler MainDS.Tables("Images").Co
Private Sub MyColChanging(ByVal sender As System.Object, ByVal e As System.Data.DataColumnChan
If Not IsDBNull(e.ProposedValue) Then ' this was needed to allow for nulls
If e.ProposedValue = "aa" Then
MsgBox("Title should not equal " & e.ProposedValue)
Throw New Exception("oops")
End If
End If
End Sub
This will show a message and satisfactorily stop "aa" from being added to the "Title" column. However when you move from the cell, whether with mouse or keyboard, try as I might, I cannot find a way to select the original cell.
Incidentally I have no idea how the "Throw New Exception" works. When I tried to use an "On Error GoTo" in the handling routine the code in the on error section would run OK, but the invalid data was allowed in the original cell. Any comments on this would be appreciated also.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thanks for the ideas, including those in RonaldBiemans replies. I had tried these already, but they do not work when triggered from within the ColumnChanging event.
For example if you are in cell(1,1) and you edit it with a value that will fail your validation, then click next on cell(4,4), despite putting code in the ColumnChanging event like Me.grdMain.CurrentRowIndex
I have developed a reasonably neat workaround if you don't hate using flags (I love flags), which I show below. I intend to ask to have this question deleted. I don't know what is is that as soon as I determine I am stuck and ask a question, I end up working out a reasonable solution.
So thanks again.
Here's my solution which may be of use to you and others. It is based on the fact that when you move to a new cell after editing the previous cell you will always fire the CurrentCellChanged event after any ColumnChanging code has finished running.
So every time the CurrentCellChanged event is fired I do one of two things. If the ReEditNeeded flag is False then I just save the new cell position in case I need to return to it. If the ReEditNeeded is True then clear the flag back to False and I select the old position.
The validation code in the ColumnChanging event is used to set the ReEditNeeded flag to True if there is a validation issue.
This works really well and does not seem to add any discernable load to the application.
' Initialise values used for data validation in the declaration area
Dim ReEditNeeded = False
Dim ReturnToCell = New DataGridCell(0, 0)
' Add event handlers for the grid for changing of current cell and the table for changing of column
AddHandler grdMain.CurrentCellChanged
AddHandler MainDS.Tables("Images").Co
Private Sub MyCellChanging(ByVal sender As System.Object, ByVal e As System.EventArgs)
' NOTE make sure you reset the flag first or you go to the cell twice (unnecessarily)
If ReEditNeeded Then
ReEditNeeded = False
Me.grdMain.CurrentCell = ReturnToCell
Else
ReturnToCell = Me.grdMain.CurrentCell
End If
End Sub
Private Sub MyColChanging(ByVal sender As System.Object, ByVal e As System.Data.DataColumnChan
Dim ErrMsg = ""
Select Case e.Column.ColumnName
Case "ImageNum"
If IsDBNull(e.ProposedValue) Then
ErrMsg = "Image # cannot be empty"
End If
Case "Title"
If Not IsDBNull(e.ProposedValue) Then ' Must allow nulls here.
If e.ProposedValue = "aa" Then
ErrMsg = "Title should not equal " & e.ProposedValue
End If
End If
End Select
' If there is a message, you must have invalid data therefore set the flag for re editing, put the old value back and show a message
If ErrMsg <> "" Then
ReEditNeeded = True
e.ProposedValue = e.Row(e.Column.ColumnName)
MsgBox(ErrMsg, MsgBoxStyle.Critical, "Invalid Data")
End If
End Sub
Regards,
Neil
>> I don't know what is is that as soon as I determine I am stuck and ask a question, I end up working out a reasonable solution.
Can you come up with a reasonable solution to the link I posted above, which is still open for one last issue.. seems like RonaldBiemans has not been coming online for some days..
Hi Rajesh,
I cannot find a reference in the MSDN Library to EndActiveEdit. I have looked at EndCurrentEdit, CancelCurrentEdit, BeginEdit and EndEdit. As far as I can tell, all of these are related to controlling the ability to edit and the acceptance or rejection of changes.
My issue is all about re-positioning the focus to a cell in which editing was occurring when the data was invalid. As I said at the start, I have no problem validating data and rejecting invalid changes. I am very happy with my solution now. I allow the user, after giving a warning, to edit the whole datagrid directly rather than record at a time on a separate form. Sort of super user stuff. So I will only add the handler when they are in this mode, the overhead then is not detectable among the normal keystrokes and clicks of editing.
Thanks anyway.
Regarding your problem, I must start by saying that I have only been experimenting with .NET for about two months in my lunch times. I have had no training and have not read any books. I have just been trying to apply my knowledge of VBA and using help and the web.
Having got that disclaimer out of the way, if I understand you correctly, you are getting a different result using the hot key to using the button. I often find a cure for such seemingly illogical problems using inelegant but satisfactory solutions.
I wonder if the difference in reactions may be explained by the change in focus that occurs when you click the button, does not occur in the same way with the hot key combination.
In your position I would be trying something like this.
As part of the Save button code, change the focus to something other than the grid, maybe even the Save button itself. If that is no good, maybe try this.
I assume for now that there is at least one existing record in the table, and hence you must be adding a record other than on row 0.
When you click Save, move back one record, then forward again.
DataGrid1.CurrentRowIndex = DataGrid1.CurrentRowIndex - 1
DataGrid1.CurrentRowIndex = DataGrid1.CurrentRowIndex +1
Now update the table.
I know that this is clunky, but hey if it works …
You may need to actually select the cell, not the row, to guarantee a suitable change of focus.
DataGrid1.CurrentCell = New DataGridCell(DataGrid1.Cur
DataGrid1.CurrentCell = New DataGridCell(DataGrid1.Cur
You can capture the column you are in if it is not 0.
If you need to be able to add a record to an empty table, you would need to start to add a second record to let you change focus of the first record, then lose the second record.
Let me know how you go.
Good luck.
Neil
Business Accounts
Answer for Membership
by: rajesh_khaterPosted on 2005-10-12 at 04:35:51ID: 15067773
Do you want to set focus on the cell, or do you want to highlight the text in the cell ?
idColumnSt yles(DataG rid.Curren tCell.Colu mnNumber)
tBox
e.com/Prog ramming/ Pr ogramming_ Languages/ Dot_Net/VB _DOT_NET/Q _21570367. html
If you want to set focus on the cell, try adding:
DataGrid.focus()
after throwing the exception.
If you want to highlight the text,
you will have to do something like this:
Dim tb as TextBox
Dim gridCol As DataGridColumnStyle = datagrid.TableStyles(0).Gr
tb = DirectCast(gridCol, DataGridTextBoxColumn).Tex
tb.SelectAll
I am writing this from memory. So some method names may not be correct.
If you use this approach of using the DataGrid, you may run into other problems. Try binding the DataGrid to a DataGridView instead.
For details, look at a question I asked on this forum:
http://www.experts-exchang
All credit to RonaldBiemans for giving me these valuable ideas on using the DataGrid.