locked
Custom control in datagridview problem RRS feed

  • Question

  • Hello,

    I have a custom numeric textbox that i've added to the datagrid view.  everything works great except Tabing off the control causes problems.  The value entered is not always saved.  The first time i enter the control/cell and modify it and tab off it saves.  All consequent times the value is lost.  It reverts back to what it was.  Has anyone encountered this?  I can post code if that will help, i just dont know what peiece since there are 4 files infolved in this custom control.
    Tuesday, February 12, 2008 4:29 PM

Answers

  • You can override DataGridView.EditingControlShowing event, where you obtain the DataGridViewTextBoxEditingControl from the EventArgs e. Add your own KeyEventHandler to control.KeyDown event.

    Here’s the source code:

    Code Snippet

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

    {

    DataGridViewTextBoxEditingControl ctl = e.Control as

        DataGridViewTextBoxEditingControl;

     

        ctl.KeyDown += new KeyEventHandler(this.dataGridView1_KeyDown);   

    }

    public void dataGridView1_KeyDown(Object sender, KeyEventArgs e)

    {

    DataGridViewTextBoxEditingControl ctl = sender as

        DataGridViewTextBoxEditingControl;

     

        if(e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)

        {

            MessageBox.Show("Numeric Keys");

            // your logic

        }   

    }

     

     

     

    Monday, February 18, 2008 7:38 AM

All replies

  • Where do you save the data the user types into your textbox control? Do you save it in your control?

    If the answer is yes, then that’s your problem.

     

    DataGridView does not instantiate your NumericTextBox control for every cell. It only creates one instance of your user control and all the cells share it. Speaking more strictly, your user control is only instantiated when a cell is in its “Edit Mode”.

     

    Friday, February 15, 2008 3:03 AM
  • You can override DataGridView.EditingControlShowing event, where you obtain the DataGridViewTextBoxEditingControl from the EventArgs e. Add your own KeyEventHandler to control.KeyDown event.

    Here’s the source code:

    Code Snippet

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

    {

    DataGridViewTextBoxEditingControl ctl = e.Control as

        DataGridViewTextBoxEditingControl;

     

        ctl.KeyDown += new KeyEventHandler(this.dataGridView1_KeyDown);   

    }

    public void dataGridView1_KeyDown(Object sender, KeyEventArgs e)

    {

    DataGridViewTextBoxEditingControl ctl = sender as

        DataGridViewTextBoxEditingControl;

     

        if(e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)

        {

            MessageBox.Show("Numeric Keys");

            // your logic

        }   

    }

     

     

     

    Monday, February 18, 2008 7:38 AM
  • Thanks guys, but what ended up working for me is setting the value of the cell at the edittingcontrol OnLostFocus event.
    Monday, February 18, 2008 2:44 PM