DataGridView 屏蔽文本框列 [英] DataGridView Masked TextBox Column

查看:30
本文介绍了DataGridView 屏蔽文本框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 2 列的 DataGridView,我想为它创建某种输入掩码.所以我找到了一个继承 maskedtextbox 控件的小类,让你在 datagridview 中使用它.一切正常,面具按预期工作,没什么大不了的.继承人图书馆:http://www.codeproject.com/Articles/26005/DataGridViewColumn-Hosting-MaskedTextBox

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: http://www.codeproject.com/Articles/26005/DataGridViewColumn-Hosting-MaskedTextBox

我的问题是,一旦该行包含我需要的所有数据,即使我有 datagridview1.AllowUserToAddRows = true,按 Enter 或 Tab 也不会创建新行.然后我发现问题出在我链接的库中,因为当我添加一个简单的数据网格文本框时,按 Enter 或 Tab 确实会创建一个新行.

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.

所以我添加了这个例程,希望当我在最后一行的最后一列时创建一个新行:

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

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

这个例程的问题在于它确实添加了一行,但它在最后一行之前创建了它,从而产生了一个间隙,就好像我在启动其他事件之前创建了一行.我应该更改 maskedtextbox 库中的某些内容,或使用不同的事件,但我不知道如何编辑以及编辑什么.

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.

这里是编辑控件的源代码:

Here is the source code of editing control:

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 & 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
}

推荐答案

当您的编辑控件发生更改时,您应该使用 NotifyCurrentCellDirty(true) 网格.所以你可以在你的编辑控件中编写这样的代码:

When a change is made in your editing control, you should notify the grid of changes using NotifyCurrentCellDirty(true) of the grid. So you can write such code in your editing control:

protected override void OnTextChanged(EventArgs e)
{
    base.OnTextChanged(e);
    EditingControlValueChanged = true;
    EditingControlDataGridView.NotifyCurrentCellDirty(true);
}

这篇关于DataGridView 屏蔽文本框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆