DataGridView屏蔽TextBox列 [英] DataGridView Masked TextBox Column

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

问题描述

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



我的问题是,一旦列有我需要的所有数据,按enter或tab不会创建一个新行,即使是我有datagridview1.AllowUserToAddRows = true。
然后我发现问题是在我链接的库,因为当我添加一个简单的datagrid文本框,按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
私有DataGridView dataGridView;
private bool valueChanged;
private int rowIndex;
#endregion
#region构造函数
public DataGridViewMaskedTextEditingControl()
{
Mask = String.Empty;
}
#endregion
#region接口的属性
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接口的方法
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
Font = dataGridViewCellStyle.Font;
//获取当前单元格使用特定的掩码字符串
DataGridViewMaskedTextCell单元格
= dataGridView.CurrentCell作为DataGridViewMaskedTextCell;
if(cell!= null)
{
Mask = cell.Mask;
}
}
public bool EditingControlWantsInputKey(Keys key,bool dataGridViewWantsInputKey)
{
//注意:在DataGridView中,可以更喜欢使用
//上/下键。
switch(key& Keys.KeyCode)
{
case Keys.Left:
case Keys.Right:
case Keys.Home:
case Keys.End:
返回true;
默认值:
返回false;
}
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void PrepareEditingControlForEdit(bool selectAll)
{
if(selectAll)
SelectAll();
else
{
SelectionStart = 0;
SelectionLength = 0;
}
}
#endregion
#region MaskedTextBox事件
protected override void OnTextChanged(System.EventArgs e)
{
base.OnTextChanged (e);
EditingControlValueChanged = true;
if(EditingControlDataGridView!= null)
{
EditingControlDataGridView.CurrentCell.Value = Text;
}
}
#endregion
}


解决方案

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

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


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

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();
    }
}

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
}

解决方案

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屏蔽TextBox列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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