Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridViewTextBoxEditingControl adding numeric text inside
 

DataGridViewTextBoxEditingControl adding numeric text inside

I have the following code


Code Snippet

/// Puts the current cell in edit mode.
private void monitorDataGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
{
validFormat = true;
monitorDataGrid.EditMode = DataGridViewEditMode.EditOnEnter;
//monitorDataGrid.BeginEdit(false);
}

/// Occurs when a control for editing a cell is showing.
/// Performs custom initialization of the editing control when a cell enters edit mode
private void monitorDataGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
mKeyUpController.Clear();
Control ctl = e.Control;

DataGridViewTextBoxEditingControl dText = (DataGridViewTextBoxEditingControl)ctl;
dText.EditingControlDataGridView.EditMode = DataGridViewEditMode.EditOnEnter;
dText.KeyDown += new KeyEventHandler(dText_KeyDown);
dText.KeyUp += new KeyEventHandler(dText_KeyUp);
}

void dText_KeyDown(object sender, KeyEventArgs e)
{
monitorDataGrid.CurrentCell.ErrorText = String.Empty;

if (e.KeyData == Keys.OemPeriod || e.KeyData == Keys.Decimal)
{

}
else if (
((int)e.KeyCode >= (int)Keys.D0 && (int)e.KeyCode <= (int)Keys.D9) ||
((int)e.KeyCode >= (int)Keys.NumPad0 && (int)e.KeyCode <= (int)Keys.NumPad9)
)
{
DataGridViewTextBoxEditingControl dText = (DataGridViewTextBoxEditingControl)sender;

e.Handled = false;

// This is needed for text to go 123 instead of 321
dText.PrepareEditingControlForEdit(false);
dText.DeselectAll();
}

}




For some reason when i run this code if i press a button and the button put an error text in my cell, when i go back , it

selects all the text (something selects it not me ) and if i type a number, it will add a number at the end of the current

number in the cell

For example
I have in a datagridviewcell the value 1.000

I press a button and puts an error text in the datagridviewcell

i go back and select the value 1.000 and if i press 2 it will put
1.001
if i press 2 again it will put
1.002
if i press 2 again it will put
1.003

** Disclaimer : There's nothing in my code that adds the current value of the cell ... I believe this is a bug of this

control

The point is that there's a bug either in my code or in the control that causes this
what should happen is that if i press 2 it should either delete 1.000 and put 2 , or simply append the 2 at the end

ANY THOUGHTS ON WHAT IS CAUSING THIS?
Daniel Lesseps  Tuesday, August 14, 2007 9:17 PM

I wrote a solution to this at

http://danielisaacs.com/2007/08/18/datagridviewcell-adding-itself/

I finally figure it out what was wrong

Daniel Lesseps  Sunday, August 19, 2007 5:36 PM

One problem I do see is that you will have multipleinstances of the keyup and keydown event handlers. You should remove the old handler before adding a new one

Code Snippet
dText.KeyDown -= new KeyEventHandler(dText_KeyDown);
dText.KeyUp -= new KeyEventHandler(dText_KeyUp);
dText.KeyDown += new KeyEventHandler(dText_KeyDown);
dText.KeyUp += new KeyEventHandler(dText_KeyUp);


Ken Tucker  Wednesday, August 15, 2007 9:31 AM

This definitely solved one of the problems, but it's still adding the numbers for some reason

Daniel Lesseps  Wednesday, August 15, 2007 4:16 PM

I wrote a solution to this at

http://danielisaacs.com/2007/08/18/datagridviewcell-adding-itself/

I finally figure it out what was wrong

Daniel Lesseps  Sunday, August 19, 2007 5:36 PM

You can use google to search for other answers

Custom Search

More Threads

• BindingContext[ds, tableName].Position
• Error when i try to save to Database
• combobox selected index -1 doesnot work on citrix machines to show the combobox empty
• TableAdapter Query Results Not Being Displayed in DataGridView
• Hash Table
• DataGridViewButtonCell - change back color of button in the cell
• DataGridView - Add column from diferent table
• DataGrid View - Ctl+0 to null out a combobox column
• DataGridView - simulating an Access subform
• How to create custom Format for DataGridView?