DataGridView Masked TextBox Column
<p>I have a DataGridView with  2 columns,and i wanted to create some kind of input mask to it. So i found a small class that inherits maskedtextbox control and lets you use it in a datagridview. Everything works fine,the mask works as intended, no big deal. heres the library: <a href="http://www.codeproject.com/Articles/26005/DataGridViewColumn-Hosting-MaskedTextBox" rel="nofollow">http://www.codeproject.com/Articles/26005/DataGridViewColumn-Hosting-MaskedTextBox</a></p>

<p>My problem is,once the row has all the data i need,pressing enter or tab does not create a new row, even tho i have datagridview1.AllowUserToAddRows = true.
Then i found out the problem was in the library i linked,because when i add a simple datagrid textbox,pressing enter or tab does create a new row.</p>

<p>so i added this routine,to hopefully create a new row when i am on the last column of the last row: </p>

<pre><code>private void dgOre_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if(e.RowIndex== dgOre.Rows.Count-1 &amp;&amp; e.ColumnIndex== dgOre.Columns.Count - 1){
            dgOre.Rows.Add();
    }
}
</code></pre>

<p>the problem with this routine is that it does add a row,but it creates it BEFORE the last row,creating a gap,as if i am creating a row before some other event is launched. I should change something in the maskedtextbox library, or use a different event,but i have no idea how and what to edit.</p>

<p>Here is the source code of editing control:</p>

<pre><code>public class DataGridViewMaskedTextEditingControl : MaskedTextBox, 
    IDataGridViewEditingControl
{
    #region Fields
    private DataGridView dataGridView;
    private bool valueChanged;
    private int rowIndex;
    #endregion
    #region Constructor
    public DataGridViewMaskedTextEditingControl()
    {
        Mask = String.Empty;
    }
    #endregion
    #region Interface's properties
    public DataGridView EditingControlDataGridView
    {
        get { return dataGridView; }
        set { dataGridView = value; }
    }
    public object EditingControlFormattedValue
    {
        get { return Text; }
        set
        {
            if (value is string)
                Text = (string)value;
        }
    }
    public int EditingControlRowIndex
    {
        get { return rowIndex; }
        set { rowIndex = value; }
    }
    public bool EditingControlValueChanged
    {
        get { return valueChanged; }
        set { valueChanged = value; }
    }
    public Cursor EditingPanelCursor
    {
        get { return base.Cursor; }
    }
    public bool RepositionEditingControlOnValueChange
    {
        get { return false; }
    }

    #endregion
    #region Interface's methods
    public void ApplyCellStyleToEditingControl(
        DataGridViewCellStyle dataGridViewCellStyle)
    {
        Font = dataGridViewCellStyle.Font;
        //  get the current cell to use the specific mask string
        DataGridViewMaskedTextCell cell
            = dataGridView.CurrentCell as DataGridViewMaskedTextCell;
        if (cell != null)
        {
            Mask = cell.Mask;
        }
    }
    public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
    {
        //  Note: In a DataGridView, one could prefer to change the row using
        //  the up/down keys.
        switch (key &amp; Keys.KeyCode)
        {
            case Keys.Left:
            case Keys.Right:
            case Keys.Home:
            case Keys.End:
                return true;
            default:
                return false;
        }
    }
    public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
    {
        return EditingControlFormattedValue;
    }
    public void PrepareEditingControlForEdit(bool selectAll)
    {
        if (selectAll)
            SelectAll();
        else
        {
            SelectionStart = 0;
            SelectionLength = 0;
        }
    }
    #endregion
    #region MaskedTextBox event
    protected override void OnTextChanged(System.EventArgs e)
    {
        base.OnTextChanged(e);
        EditingControlValueChanged = true;
        if (EditingControlDataGridView != null)
        {
            EditingControlDataGridView.CurrentCell.Value = Text;
        }
    }
    #endregion
}
</code></pre>