-
May 27th, 2024, 10:48 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Problems with editing a DataGridView
I have a DataGridView.
The user can type a query (for example a select query) in a Textbox and run it, and the DataGridView shows the resulting records.
Now, lets say the user, then clicks on a cell in an INTEGER column and starts to type a non-INTEGER, for example a text.
This is not something that the user is supposed to do, but, let's say he does this by mistake.
As a result of this unwarranted editing of the cell, nothing happens at that moment, but then when the user tries to move away from that grid (for example click on another control (for example a textbox)) then a large error message shows and blocks the user from moving away from that DataGridView.
Even trying to close the application is also impossible, because when the user clicks on the x box (top right of the form's title-bar), that same error message pops up and after clicking OK, the application stays open and the focus is set to that cell in the DataGridView.
The only way to get out of this lock-up is to re-edit that cell and enter an integer. Then the user can close the app or move away from that DataGridView.
That large error message also says that I can replace it (replace that error message) by handling the DataGridView's DataError event.
So, I handle that event like this:
Code:
private void dgvSqlQueryGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show(string.Format("Error: {0}", e));
}
But, the problem remains. That large error message no longer shows, and instead the above small message shows, but still the user cannot move away from that DataGridView (not even close the app) until he re-edits that DataGridView cell and enters an integer.
How can I resolve this problem?
What I need to do is that the wrong value entered by the user should be TOTALLY IGNORED and moving away from the DataGridView be allowed.
One way to avoid (!!!???) this problem is to set the DataGridView to read-only, but I really want to learn to do it without setting the DataGridView to read-only.
Once I learn how to do it, then I will set it to read-only, but first thing I need to understand how to instruct the DataGridView to IGNORE the wrong value entered.
Please advise.
Thanks.
-
May 28th, 2024, 04:36 AM
#2
Re: Problems with editing a DataGridView
I believe that you should be handling the CellValidating event. Setting e.Cancel to True will prevent the entered value being pushed to the data source and thus prevent the data error you're currently seeing. You'll probably need to provide some other way to notify the user, to ensure they don't assume the data was accepted when it wasn't.
-
May 29th, 2024, 08:26 AM
#3
Thread Starter
Fanatic Member
Re: Problems with editing a DataGridView
 Originally Posted by jmcilhinney
I believe that you should be handling the CellValidating event. Setting e.Cancel to True will prevent the entered value being pushed to the data source and thus prevent the data error you're currently seeing. You'll probably need to provide some other way to notify the user, to ensure they don't assume the data was accepted when it wasn't.
Thanks for your help.
But, that made things much worse.
To demonstrate the issue, I created a simplified version of my code in a test project:
In here, the user clicks on a button to show students in a grid (very simple version of the original code).
It works fine.
Then I edit an integer column in the grid and enter some text:
It pops up an error and blocks me from moving away from the grid or even to another cell in the grid, or even close the app!
Then I add this code:
Code:
private void dgvStudents_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
e.Cancel = true;
}
And run the app. Then I click on that button, and instead of populating the grid, it gives me this error:
https://i.imgur.com/ozlCRaq.jpeg
Then I comment out that line:
Code:
private void dgvStudents_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
//e.Cancel = true;
}
Then I run the app, and click on that button.
Now, it works fine again (like before) and populates the grid.
But, still the original problem exists:
The user cannot modify an integer cell and place an INVALID value (like a text value) and get away with it, meaning being able to place that invalid value in there and move to other controls on the form, and be 100% sure that the new value that he has placed in there will be TOTALLY IGNORED.
Mind you, the user may even enter a TOTALLY VALID value (another integer), but, I want that new value to be ignored. I want to block any attempt to even try to update the database; Not even to BEGIN to try to update the database. Just ignore any editing.
That thing that I want to do to instruct the grid to totally ignore the editing done in its cells is still something that I don't know how to do.
Please help.
Thanks
Ilia
-
Jun 2nd, 2024, 07:35 AM
#4
Thread Starter
Fanatic Member
Re: Problems with editing a DataGridView
Any help with this (any response to the above post#3) will be greatly appreciated.
Thanks again.
Ilia
-
Jun 2nd, 2024, 08:41 AM
#5
Re: Problems with editing a DataGridView
Code:
this.dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
-
Jun 3rd, 2024, 07:26 AM
#6
Thread Starter
Fanatic Member
Re: Problems with editing a DataGridView
 Originally Posted by OptionBase1
Code:
this.dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
Thanks a lot for your help.
I did this:
Code:
this.dgvStudents.EditMode = DataGridViewEditMode.EditProgrammatically;
But what it does is that it blocks the user from editing the contents of the grid (and of course if editing is disabled, that error will not happen).
Instead of doing that, I can alternatively do this:
Code:
dgvStudents.ReadOnly = true;
This too, has the exact same effect: It blocks the user from editing the contents of the grid (and of course if editing is disabled, that error will not happen).
But, how can I make editing perfectly available to the user, allow the user to enter any INVALID value and STILL not get any error?
In other words how can I instruct the DataGridView to IGNORE what the user enters in a cell?
Please advise.
Thanks again
-
Jun 3rd, 2024, 08:43 AM
#7
Re: Problems with editing a DataGridView
Inside of the DataError event of the DataGridView, try this:
Code:
dgvStudents.CancelEdit();
e.Cancel = True;
-
Jun 8th, 2024, 07:58 AM
#8
Thread Starter
Fanatic Member
Re: Problems with editing a DataGridView
 Originally Posted by OptionBase1
Inside of the DataError event of the DataGridView, try this:
Code:
dgvStudents.CancelEdit();
e.Cancel = True;
Thanks a lot for your help.
Yes. It works.
Now, if an error happens (the user enters an invalid value) the error message does not pop up and the original value of that cell is automatically restored and displayed.
But, how can I do the same thing for when the user enters a perfectly valid value as well?
In other words, for example the Student Num is 1003.
The user changes that to 576
That is a valid value and causes no error. But how can I instruct the program to restore the original value (1003) in that cell when the user tabs out?
Please advise.
Thanks again.
-
Jun 8th, 2024, 08:24 AM
#9
Re: Problems with editing a DataGridView
Why are you letting the user edit the data if they're not allowed to edit the data?
-
Jun 8th, 2024, 09:32 AM
#10
Re: Problems with editing a DataGridView
 Originally Posted by IliaPreston
Thanks a lot for your help.
Yes. It works.
Now, if an error happens (the user enters an invalid value) the error message does not pop up and the original value of that cell is automatically restored and displayed.
But, how can I do the same thing for when the user enters a perfectly valid value as well?
In other words, for example the Student Num is 1003.
The user changes that to 576
That is a valid value and causes no error. But how can I instruct the program to restore the original value (1003) in that cell when the user tabs out?
Please advise.
Thanks again.
Spend time explaining exactly why you think this is necessary. You've been given a multitude of suggestions. There's absolutely no legitimate reason to let the user edit cells if you just want to abandon their changes. None.
Good luck.
-
Jun 8th, 2024, 10:11 AM
#11
Re: Problems with editing a DataGridView
Charity footnote from me. You can probably accomplish this be dropping a dgv.CancelEdit() in some additional appropriate event(s) for the DGV itself, but you'll have to figure out which events and if you have to account for scenarios where that event might be triggered when an edit didn't happen, etc...
Good luck.
-
Jun 9th, 2024, 06:11 AM
#12
Thread Starter
Fanatic Member
Re: Problems with editing a DataGridView
 Originally Posted by OptionBase1
Spend time explaining exactly why you think this is necessary. You've been given a multitude of suggestions. There's absolutely no legitimate reason to let the user edit cells if you just want to abandon their changes. None.
Good luck.
Thanks for your reponse.
Jmcilhinney has asked the exact same question.
So, I will just respond to his post.
Cheers.
-
Jun 9th, 2024, 06:38 AM
#13
Thread Starter
Fanatic Member
Re: Problems with editing a DataGridView
 Originally Posted by jmcilhinney
Why are you letting the user edit the data if they're not allowed to edit the data?
Thanks for your response.
Let's begin with a textbox instead of a DataGridView:
If you have a textbox and you set its ReadOnly property to True, then at run time, you can click ANYWHERE you wish on it (the cursor will land in that specific point in the middle of the text that it displays), and you can highlight ANY PART of that text and copy it (copy only the highlighted part of the text and not the whole text).
And also you can do that (do the copying) either by keyboard (Ctrl+C) or by mouse (right-click then Copy).
That is what read-only means to me: No change to the data, but full EDIT access (read-only EDIT (navigating through the text, highlighting at will, copying, etc. except changing anything)) is allowed.
Now, try to do the same thing to a DataGridView:
I set the DataGridView's Read-only property to True, and expect the same behavior.
But, the behavior is NOT the same !!!
With a DataGridView's Read-only property set to True, at run time, I cannot highlight any part of the cell that I choose. If I click on a cell, the whole cell gets highlighted (no way to highlight any part of it instead of the whole of it).
And I can copy the whole content of the cell (and not any part of it).
And even that (copying the whole content of the cell) can only be done by keyboard (Ctrl+C). Mouse right-click doesn't work.
All of the above makes me think maybe I should set the DataGridView's Read-only property to False, and thereby allowing the user to make any changes that he wants, but then discard those changes and restore the original content of the cell as soon as he leaves the cell.
Is there a better way?
Please advise.
Thanks again.
Ilia
-
Jun 9th, 2024, 07:35 AM
#14
Re: Problems with editing a DataGridView
Code:
private void dgvStudents_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
dgvStudents.CancelEdit();
}
-
Jun 9th, 2024, 08:31 AM
#15
Re: Problems with editing a DataGridView
Did you really have to write all that to say "I want to be able to select and copy arbitrary text in each cell"? If you'd just said that in the first place then we could have avoided wasting days on this. This is why you ALWAYS provide a FULL and CLEAR explanation of the problem. What you're actually rtying to achieve, rather than just how you're trying to achieve it, is part of that.
You talk about how a TextBox works. When you edit a cell in a DataGridViewTextBoxColumn, you do so in a TextBox that the grid creates and embeds in the cell. If you want the cell to act like a TextBox with its ReadOnly property to True, simply set the ReadOnly property of that editing control to True. Handle the EditingControlShowing event of the grid, get the e.Control property, cast it as type TextBox and set its ReadOnly property to True. Done.
-
Jun 10th, 2024, 07:34 AM
#16
Thread Starter
Fanatic Member
Re: Problems with editing a DataGridView
 Originally Posted by OptionBase1
Code:
private void dgvStudents_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
dgvStudents.CancelEdit();
}
Yes. It works.
Thanks a lot for your help.
-
Jun 10th, 2024, 07:45 AM
#17
Thread Starter
Fanatic Member
Re: Problems with editing a DataGridView
 Originally Posted by jmcilhinney
Did you really have to write all that to say "I want to be able to select and copy arbitrary text in each cell"? If you'd just said that in the first place then we could have avoided wasting days on this. This is why you ALWAYS provide a FULL and CLEAR explanation of the problem. What you're actually rtying to achieve, rather than just how you're trying to achieve it, is part of that.
You talk about how a TextBox works. When you edit a cell in a DataGridViewTextBoxColumn, you do so in a TextBox that the grid creates and embeds in the cell. If you want the cell to act like a TextBox with its ReadOnly property to True, simply set the ReadOnly property of that editing control to True. Handle the EditingControlShowing event of the grid, get the e.Control property, cast it as type TextBox and set its ReadOnly property to True. Done.
Yes. Your suggestion works fine.
Here it is:
Code:
private void dgvStudents_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
((TextBox)(e.Control)).ReadOnly = true;
}
And it works fine.
Thanks a lot for your help.
Did you really have to write all that to say "I want to be able to select and copy arbitrary text in each cell"?
Let me apologize for that in the strongest possible terms.
Well, I would say yourself, OptionBase1 and others who respond to questions and problems on this forum are highly knowledgeable and very skillful programmers.
Please don't compare me with yourself, OptionBase1 and similar high ranking members.
I am just a humble junior programmer who is just getting along and learn a new language (C#), and I certainly make mistakes for example by bad explanation of a problem that could have been better explained in the first place to receive a quicker answer.
Anyway, I am sorry if my original explanation was bad and resulted in excessive communication.
Also, thanks again for your help and thanks to OptionBase1 for his help.
Ilia
-
Jun 10th, 2024, 07:55 AM
#18
Re: Problems with editing a DataGridView
All's well that ends well.
-
Jun 10th, 2024, 10:24 PM
#19
Thread Starter
Fanatic Member
Re: Problems with editing a DataGridView
As I said in post #17, this code:
Code:
private void dgvStudents_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
((TextBox)(e.Control)).ReadOnly = true;
}
worked perfectly for my project.
But then I copied it to another solution/project, and it gave me this compile-time error:
Error CS0104 'TextBox' is an ambiguous reference between 'System.Windows.Forms.TextBox' and 'System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox'
So, basically it looks like it is asking me to change TextBox to either 'System.Windows.Forms.TextBox' or 'System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox'
I changed that to this (let's call it code A):
Code:
((System.Windows.Forms.TextBox)(e.Control)).ReadOnly = true;
And it worked perfectly as expected.
Then I tried this (let's call it code B):
Code:
((System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox)(e.Control)).ReadOnly = true;
This one, however, doesn't work. It gives me this compile-time error:
Error CS0716 Cannot convert to static type 'VisualStyleElement.TextBox'
I don't understand why.
I can certainly use the above code A because it works perfectly.
But, it is like a mystery.
1- Why the original code:
Code:
((TextBox)(e.Control)).ReadOnly = true;
works fine in the first project, but in the second project it gives that error CS0104 ('TextBox' is ambiguous ...)
2- Why out of the two options that that error offers me, the first one (System.Windows.Forms.TextBox) works, but the second one gives that error CS0716 (Cannot convert ...)?
Please note that both of these two solution/projects are .Net Core (Neither one is .Net Framework).
Can you please shed some light on this?
Thanks a lot.
-
Jun 11th, 2024, 01:08 AM
#20
Re: Problems with editing a DataGridView
If you have imported two namespaces that both have a member type named TextBox then the compiler will not know which you intended to use if you just use TextBox unqualified in your code. You need to either remove the other namespace import if you don't need it or else qualify the type name in your code. You obviously want System.Windows.Forms.TextBox, because that's type of control you add to your form in the designer and that's the type of control a DataGridView uses to edit cells. System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox is a completely different class for a completely different purpose. If you read the documentation for that class (which you should already have done) then you will learn what it's purpose is. Also, as the error message says, that class is static, which means that you cannot create an instance of it, so you can't cast an object reference as that type because you can't create an object of that type.
-
Jun 19th, 2024, 03:54 AM
#21
Thread Starter
Fanatic Member
Re: Problems with editing a DataGridView
Thanks for all the help to everybody who helped me in here.
I am going to set this thread to Resolved.
-
Jun 20th, 2024, 08:03 PM
#22
Re: Problems with editing a DataGridView
 Originally Posted by IliaPreston
Well, I would say yourself, OptionBase1 and others who respond to questions and problems on this forum are highly knowledgeable and very skillful programmers.
Given that jmc-->Yoda, I'm-->Some random insect living on Watto.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|