Results 1 to 7 of 7

Thread: [RESOLVED] DataGridView programatically editing current cell

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2011
    Posts
    39

    Resolved [RESOLVED] DataGridView programatically editing current cell

    Hi

    I have a DataGridView and want to update the contents of the CurrentCell while the user is typing.

    e.g. In a cell of fractions I want the user to be able to enter .957 - As soon as the user types the . I want to programatically change it to 0. without the user being aware of the change.

    Thanks

  2. #2

    Thread Starter
    Member
    Join Date
    Dec 2011
    Posts
    39

    Re: DataGridView programatically editing current cell

    Just to clarify...

    Here is a TextBox example of what I want to do in my DataGridView:

    vb Code:
    1. Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    2.             If TextBox1.Text = "." Then
    3.                 TextBox1.Text = "0."
    4.                 TextBox1.Select(2, 0)
    5.             End If
    6. End Sub

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataGridView programatically editing current cell

    You do it exactly the same way with a DGV because the editing actually IS taking place in a TextBox. The DGV is just a rendering of data. It contains no controls by default and the user doesn;t edit any data in the grid itself. When the user starts an editing session, a control is created or an existing control is used if one exists and it is embedded in the cell. That control will be TextBox, ComboBox or whatever, depending on the type of the cell. The Value property of the cell is copied to the appropriate property of the control, e.g. TextBox.Text or ComboBox.SelectedValue. the user edits the data in the control and then, when they're done, the appropriate property of the control is copied back to the Value of the cell and the control is removed. That's why the cell Value does change until all the editing is done and not during.

    So, in your case, you to use the pretty much the exact same code as you posted. You might want to change the method name but the important change is that you will use the 'sender' parameter in the event handler to access the TextBox rather than a direct reference to TextBox1.

    You'll need to handle the EditingControlShowing event of the grid, which is raised when an editing session starts. You can then use the AddHandler statement to attach the method to the TextChanged event of the TextBox being embedded in the cell. You'll obviously want to make sure that the editing is occurring in the appropriate column first. You'll also want to handle the CellEndEdit event and use RemoveHandler to detach the event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2011
    Posts
    39

    Re: DataGridView programatically editing current cell

    Thanks, that's a great reply, but I just got it working using:
    vb Code:
    1. If myDataGrid.CurrentCell.EditedFormattedValue.ToString = "." Then
    2.                 myDataGrid.EditingControl.Text = "0."
    which isn't too far away from what you were explaining.

  5. #5
    Lively Member
    Join Date
    Dec 2006
    Posts
    81

    Re: [RESOLVED] DataGridView programatically editing current cell

    I have same type of issues but nibbles in which event u posted this code to get the editcontrol value

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2011
    Posts
    39

    Re: [RESOLVED] DataGridView programatically editing current cell

    If I understand your question correctly:-

    The code you are referring to is located in: myData1_CellValueChanged

    Where 'myData1_CellValueChanged' is an event handler for myDataGridView

    i.e.
    Code:
        Private Sub myData1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
                    If myDataGrid.CurrentCell.EditedFormattedValue.ToString = "." Then
                        myDataGrid.EditingControl.Text = "0."
                        SendKeys.Send("{RIGHT}{RIGHT}")
                    End If
        End Sub

  7. #7
    Lively Member
    Join Date
    Dec 2006
    Posts
    81

    Re: [RESOLVED] DataGridView programatically editing current cell

    Thanks Nibbles.
    Your did it correctly.
    But my issue differs from urs
    U need to change the value of the cell after the editing ends but i need to read the value of the editing control for each and every keypress and to manipulateit before the user ends editing.
    i found the solution from jmcilhinney another post

    http://www.vbforums.com/showthread.p...3&goto=newpost

    refer this u will get a idea of the difference between our issues.

    But any how ur code will help me at a certain stage
    thanks

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