|
-
Feb 10th, 2012, 01:26 PM
#1
Thread Starter
Member
[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
-
Feb 10th, 2012, 03:10 PM
#2
Thread Starter
Member
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:
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If TextBox1.Text = "." Then
TextBox1.Text = "0."
TextBox1.Select(2, 0)
End If
End Sub
-
Feb 10th, 2012, 11:06 PM
#3
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.
-
Feb 10th, 2012, 11:22 PM
#4
Thread Starter
Member
Re: DataGridView programatically editing current cell
Thanks, that's a great reply, but I just got it working using:
vb Code:
If myDataGrid.CurrentCell.EditedFormattedValue.ToString = "." Then
myDataGrid.EditingControl.Text = "0."
which isn't too far away from what you were explaining.
-
Oct 5th, 2012, 02:56 AM
#5
Lively Member
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
-
Oct 5th, 2012, 09:39 PM
#6
Thread Starter
Member
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
-
Oct 6th, 2012, 12:33 AM
#7
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|