【问题标题】:DataGridView Masked TextBox ColumnDataGridView 屏蔽的文本框列
【发布时间】:2016-08-10 14:24:41
【问题描述】:

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

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

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

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 库中的某些内容,或者使用不同的事件,但我不知道如何编辑以及编辑什么。

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

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
}

【问题讨论】:

  • 主要问题在DataGridViewMaskedTextEditingControl类中,在OnTextChanged方法中。

标签: c# winforms datagridview maskedtextbox


【解决方案1】:

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

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

【讨论】:

  • 好的,非常感谢。我明天会测试它,因为我失业了,但这应该是解决方案
  • 我相信这个库需要更多的修复,例如InitializeEditingControl 中的InitializeEditingControl 中的DataGridViewMaskedTextCell 类是完全错误的。 Value.ToString() 更好。更正确的方法是var str = initialFormattedValue as string; 然后if (str==null) str = string.Empty; 然后使用editingControl.Text = str;
  • 图书馆有一些明显的问题,但它足以满足我的需要。我设法进行了更多测试,您的修复确实有效,因此我将其标记为已接受,非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2015-10-02
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多