添加小数点&在KeyPress事件上将光标位置设置为DataGridView单元格文本

时间:2016-11-10 12:14:39

标签: c# winforms datagridview

以下代码用于在DataGridView单元格中的5位数之后添加小数点,并将光标位置设置为最后,但此代码仅适用于第一个DataGridView单元格。
提前致谢。

Result Image

Grid EditingControlShowing事件代码:

private void grdCharges_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    DataGridView grd = sender as DataGridView;
    e.Control.KeyPress -= new KeyPressEventHandler(grdCharges_row0_Column2_KeyPress);
    e.Control.KeyPress -= new KeyPressEventHandler(grdCharges_row1_Column2_KeyPress);
    e.Control.KeyPress -= new KeyPressEventHandler(grdCharges_Column3_KeyPress);
    if (grdCharges.CurrentRow.Index == 0)
    {
        if (grdCharges.CurrentCell.ColumnIndex == 2) //Desired Column
        {
            TextBox r0c2 = e.Control as TextBox;
            if (r0c2 != null)
            {
                r0c2.KeyPress += new KeyPressEventHandler(grdCharges_row0_Column2_KeyPress);
            }
        }
    }
    if (grdCharges.CurrentRow.Index == 1)
    {
        if (grdCharges.CurrentCell.ColumnIndex == 2) //Desired Column
        {
            TextBox r1c2 = e.Control as TextBox;
            if (r1c2 != null)
            {
                r1c2.KeyPress += new KeyPressEventHandler(grdCharges_row1_Column2_KeyPress);
            }
        }
    }
    if (grdCharges.CurrentCell.ColumnIndex == 3) //Desired Column
    {
        TextBox tb3 = e.Control as TextBox;
        if (tb3 != null)
        {
            tb3.KeyPress += new KeyPressEventHandler(grdCharges_Column3_KeyPress);
        }
    }
}

将小数点添加到gridview数据值:

private void grdCharges_row0_Column2_KeyPress(object sender, KeyPressEventArgs e)
{

    VaildationGrdCharges(sender, e, grdCharges.CurrentRow.Index, grdCharges.CurrentCell.ColumnIndex);
    TextBox textBox = (TextBox)sender;
    if (textBox.Text.Length.ToString() == "5")
    {
        grdCharges.Rows[grdCharges.CurrentRow.Index].Cells[grdCharges.CurrentCell.ColumnIndex].Value = string.Concat(textBox.Text, ".");
        if (grdCharges.CurrentCell.EditType == typeof(DataGridViewTextBoxEditingControl))
        {
            ((TextBox)this.grdCharges.EditingControl).SelectionStart = textBox.Text.Length + 2; // add some logic if length is 0
           // ((TextBox)this.grdCharges.EditingControl).SelectionLength = 0;
        }
    }
}

private void grdCharges_row1_Column2_KeyPress(object sender, KeyPressEventArgs e)
{
    VaildationGrdCharges(sender, e, grdCharges.CurrentRow.Index, grdCharges.CurrentCell.ColumnIndex);                
    TextBox textBox = (TextBox)sender;
    if (textBox.Text.Length.ToString() == "5")
    {
        grdCharges.Rows[grdCharges.CurrentRow.Index].Cells[grdCharges.CurrentCell.ColumnIndex].Value = string.Concat(textBox.Text, ".");
        if (grdCharges.CurrentCell.EditType == typeof(DataGridViewTextBoxEditingControl))
        {
            ((TextBox)this.grdCharges.EditingControl).SelectionStart = textBox.Text.Length + 2; // add some logic if length is 0
            //((TextBox)this.grdCharges.EditingControl).SelectionLength = 0;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  

此代码仅适用于第一个网格视图单元格?

     

我只需要一个线索或答案或建议。

因为您删除所有事件处理程序:

e.Control.KeyPress -= new KeyPressEventHandler(grdCharges_row0_Column2_KeyPress);
e.Control.KeyPress -= new KeyPressEventHandler(grdCharges_row1_Column2_KeyPress);
e.Control.KeyPress -= new KeyPressEventHandler(grdCharges_Column3_KeyPress);

但是,根据检查CurrentCell Row和Columnm索引的if语句条件,您只需添加一个,例如:

r0c2.KeyPress += new KeyPressEventHandler(grdCharges_row0_Column2_KeyPress);

您可以使用Debug.WriteLine看到丢失的事件,在我的脑海中示例:

System.Diagnostics.Debug.WriteLine("SomeEvent has " + SomeEvent.GetInvocationList().Length + " event handlers");

所以修复它的问题需要Hans建议并使用CellValidating事件来设置光标。