CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2012
    Posts
    4

    Smile 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!

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: dataGridView cells formatting

    [ redirected ]
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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.

  5. #5
    Join Date
    Sep 2012
    Posts
    4

    Red face Re: dataGridView cells formatting

    Quote Originally Posted by VictorN View Post
    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.

  6. #6
    Join Date
    Sep 2012
    Posts
    4

    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!

  7. #7
    Join Date
    Sep 2012
    Posts
    4

    Smile 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!

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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?

    Quote Originally Posted by Mathend View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured