Results 1 to 3 of 3

Thread: [RESOLVED] Edit DataGridView Cell text programatically

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2020
    Posts
    7

    Resolved [RESOLVED] Edit DataGridView Cell text programatically

    I have created a popup keyboard to use on touch screens that works perfectly when editing a standard textbox.
    However I need it to work when typing text into a DataGrid cell.
    I have used some code that JMcilhinney had written elsewhere that came close to solving my problem and it involves casting a Datagrid editing control to a textbox.
    Code:
    FreeText_TextBox = DirectCast(dgvFoodSelection.EditingControl, TextBox)
    The issue I have is I can't get the caret/cursor to move around in the cell like I can in a normal texbox i.e. using SelectionStart and SelectionLength etc
    I'm pretty sure the reason is because I am using the cells value property to update the cell after each "on screen keyboard" button click/touch
    Code:
    dgvFoodSelection(col, row).Value = FreeText_TextBox.Text
    I figure there must be a way to update the cell contents without using value i.e. using an event of some sort but i'm not clever enough work it out.
    Any help would be much appreciated.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Edit DataGridView Cell text programatically

    Quote Originally Posted by deanobravo View Post
    The issue I have is I can't get the caret/cursor to move around in the cell like I can in a normal texbox
    Of course you can't. There is no caret in the cell. A DataGridView doesn't contain any controls by default. It is simply a rendering of text and, in some cases, controls. It's only when you start editing a cell that a control is created and embedded in that cell. That control - the one your first code snippet refers to - is just like any other control, so you can manipulate the caret in it like any other control. That's not the cell though. If you're not editing the cell then there is no control so there is no caret.

    If you want to put the grid in edit mode programmatically then you can call its BeginEdit method. That will embed an editing control in the current cell, just like if the user started editing. You can then access that editing control and use it like any other control. When you're done, call EndEdit and the editing control will be removed and its contents pushed back to the cell.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2020
    Posts
    7

    Re: Edit DataGridView Cell text programatically

    I was almost there.
    It was the BeginEdit Method which I was trying to use but didn't quite have it right.
    Turns out I needed to call the method each time before updating the textbox's text property.
    Thanks for the push.

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