Click here to Skip to main content
15,889,732 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want my c# program with datagridview to work almost exaktly like SQL Server Management Studio if you click on "Edit top 200 Rows" in the context menu of a table.
There is no "Save-Button" and you can't put the focus on a other row, if data are false.

Before the focus is left from a row in a datagridview, a verification in C# shall be done, in which the focus loose can be canceled. Something like Before_Row_Update. CellValidating is not working, because it prevents the user from entering data in a different column.

Example: two columns, both have to be filled with data. User enters value in Col 1 and then klicks on other row. Now verification has to be done (error because Col 2 is empty) and the focus shall stay in Col1 but the user must be able to set the focus on Col 2. It would be better if the focus is set automatically to Col 2.

I thought i could use the RowLeaveEvent, but this Event seems not to be cancellable.

Looking forward :-)
Posted
Updated 19-Feb-15 1:30am
v2

I can imagine the trouble you could cause if this event could be cancelled. So good for you it cannot. :-)
This approach is plain wrong. Instead, you should just mark data invalid (say, show some visual feedback). Invalid data is harmless, up to the point you are trying to use it. For example, if you have some button which is handled the way the data can be used. In this case, when you detect that data is invalid, disable that button. Or something else.

—SA
 
Share this answer
 
Comments
Philofreak 18-Feb-15 17:37pm    
I don't understand what trouble there should be, if the RowLeaveEvent could be cancelled. I don't want to have a button to save the data, this shall be done like in MS-Access. If you set focus to a other row or leave the datagridview, it shall be saved automatic. But thx for your help!
Sergey Alexandrovich Kryukov 18-Feb-15 17:59pm    
Then think about it. Do you think the user will always be able to fix the data validity not leaving the line? What makes you think so? The event lacks cancellation by a good reason. Listen to a good friendly advice: don't do it. The right solution is obvious, and already many developers suffered from the same wrong idea you got.

If you don't want to change your approach, it will be your choice, please deal with it by yourself.

—SA
Philofreak 18-Feb-15 18:36pm    
Hi Sergey, i hope and guess we missunderstand each other!
Of course data validation has to be done programmatically.
But I don't want to do this after a cell is updated.
Because if user enters a value in column 1 and then clicks on a other record, and column 2 also has to have a value, this row will contain false data.
So if the user clicks on a other row or sets the focus outside of the datagridview, data has to be validated. And if data is wrong, the focus shall not go to the other record but stay in the row. So if the Row_Exit Event could be cancelled it would be helpful.
But the user shall be able to set focus on column 2.
I hope now you understand!
Sergey Alexandrovich Kryukov 18-Feb-15 19:22pm    
Frankly, this is exactly how I understood you in first place. And still think this is wrong. I really used to experiment with the scenarios when change of selection is prevented (more exactly, not that but prevention of focus shift, but these different scenarios have a lot in common or could be combined). I don't really want to argue much about it. I'll just say, if you want to stay out of trouble, you can analyze all possible outcomes from the schema you are suggesting (I'm not trying to convince you that you should believe just my words) and come to the same conclusion I did. Mind you, I am not saying this will never work. I can imaging simple cases when it would be fine, but this is not enough reason for doing that. I already gave you a hint: how can you be sure that the validation can be restored without leaving the current selection. If this is presently the case, it may break after slight modification of your code. Just think about it.

From the other hand, what problems do you see in my suggestion? Extra click to select the line? Not enough visual feedback — but then provided it, highlight somehow all problematic elements and re-select, say, first of them after showing the error message (which would be pretty close to your own idea, just less fragile). My schema is more general and less fragile, try to understand it.

—SA
Philofreak 19-Feb-15 7:23am    
I want my program to work almost exaktly like SQL Server Management Studio if you click on "Edit top 200 Rows" in the context menu of a table.
There is no "Save-Button" and you can't put the focus on a other row, if data are false.
Sergey, maybe you now understand? :-)
If the RowValidate Event is canceled, then the focus can't be set to another row.
I haven't seen this, because RowLeave fires before RowValidate. Still don't know why this is the order.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Feb-15 11:24am    
No, selection of one or another row in not focus.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900