-
September 10th, 2012, 02:06 AM
#1
dataGridView cells formatting
Hi, guys. Sorry to bother you, but I need a little help.
I have a very simple dataGridView control (4 columns, 3 of them read-only). I need:
1) to keep users from entering characters other than 0 and natural numbers (1 to 20) on column 3. Currently, it's formatted as numeric, zero decimals, but accepts anything, including spaces.
2) to have row 0, column 3 as the selected cell when the DGV first opens, so the user can start entering information right away. So far, these instructions:
Code:
this->dataGridView1->ClearSelection();
this->dataGridView1->Rows[0]->Cells[3]->Selected = true;
are not working.
I'd appreciate your help. Thank you very much!
-
September 10th, 2012, 04:19 AM
#2
Re: dataGridView cells formatting
-
September 10th, 2012, 04:37 AM
#3
Re: dataGridView cells formatting
What dataGridView do you mean? Is it a .NET (Windows Forms) control? If it is the case that you should ask about it in the Managed C++/CLI forum.
If not - then provide more info about this control.
Victor Nijegorodov
-
September 10th, 2012, 10:55 AM
#4
Re: dataGridView cells formatting
Originally Posted by VictorN
What dataGridView do you mean? Is it a .NET (Windows Forms) control? If it is the case that you should ask about it in the Managed C++/CLI forum.
If not - then provide more info about this control.
My bad. I wasn't sure this was the right forum, for it's a .NET Windows Form Control and I read somewhere one cannot post the same questions at different forums.
-
September 10th, 2012, 07:24 AM
#5
Re: dataGridView cells formatting
Your problem to set the selection is probably related to the fact that, in order to allow a user to edit cell content, the cell does not just need to be selected, it needs to be the current cell. There can be more than one selected cell at a time but at most one current cell. This is how I did that successfully, perhaps you try that:
Code:
dataGridView1->CurrentCell = dataGridView1->Rows[0]->Cells[3];
Validation of the numeric data input apparently does not occur before the user submits the edit (e.g. by hitting Enter) or even the DGV submits the data to the underlying database it seems to have. Yet the cell still seems to be a DataGridViewTextBoxCell, accepting any user input an ordinary textbox would. To constrain the keys accepted from the user, this procedure might help:
Attach a CellBeginEdit event handler to the data grid view. (At this point you probably don't need to check whether the cell that's about to be edited actually is one that needs to be constrained to numeric input, since it seems to me that the other cells aren't editable anyway.) Now, from inside the begin edit handler, attach a KeyPress event handler to the DGV's EditingControl which simply sets the e->Handled it got passed to true whenever it sees a character you don't want to allow the user to enter.
Somewhat complicated, but I'm afraid that's as easy as it gets. There certainly are other ways to do this, mostly involving deriving your own cell class from DataGridWiewTextBoxCell or even DataGridViewCell, which give you more control about what's actually going on, but they're also likely to be much more complicated.
Furthermore, whether the input filtering method I outlined above can work at all, depends on whether the DGV already has created its editing control when the CellBeginEdit event gets fired, which I didn't get clearly from the MSDN docs and didn't check myself.
You have seen that the data grid view is extremely powerful, but unfortunately this comes at the price that this single control is considerably more complex than some entire simpler programming languages...
HTH
Last edited by Eri523; September 10th, 2012 at 11:36 AM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
September 10th, 2012, 11:24 AM
#6
Re: dataGridView cells formatting
@Eri523:
Thanks so much for your reply. Before posting my question, I browsed through some of the events and CellBeginEdit caught my eye. Problem is, there are so many properties, methods and events that I get confused, and msdn doesn't always have code examples for C++ (mostly VB &C#). Let me read your post several more times and try all your suggestions. More complicated problems have been solved before. THANKS A LOT, AGAIN!
-
September 11th, 2012, 01:44 AM
#7
Re: dataGridView cells formatting
PROBLEM SOLVED!!! Thanks a lot, guys!. It turned out to be much easier than I thought, and I learned a few things along the way.
I was looking for an icon, a button, a box or something to mark this question as solved, but I don´t see anything of the kind. Thanks again!
-
September 11th, 2012, 06:18 AM
#8
Re: dataGridView cells formatting
You're welcome!
Did you use the method with the KeyPress handler described above? If yes, that would confirm to me that it actually works. Otherwise, how did you do it?
Originally Posted by Mathend
I was looking for an icon, a button, a box or something to mark this question as solved, but I don´t see anything of the kind.
It's in the Thread Tools menu at the top of the thread display.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
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
|