关于c#:DataGridView蒙版的文本框列

DataGridView Masked TextBox Column

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

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

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

1
2
3
4
5
6
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库中的某些内容,或者使用其他事件,但是我不知道如何以及如何编辑。

这是编辑控件的源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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)通知网格更改。因此,您可以在编辑控件中编写以下代码:

1
2
3
4
5
6
protected override void OnTextChanged(EventArgs e)
{
    base.OnTextChanged(e);
    EditingControlValueChanged = true;
    EditingControlDataGridView.NotifyCurrentCellDirty(true);
}