将DataGridView列限制为2位小数

时间:2010-09-21 05:24:37

标签: c# datagridview

嗨我有一个gridview.My要求是当用户在字段中输入一个十进制值时,它应该允许用户只输入2位小数位。输入2位小数后,焦点应该到达下一个字段。 感谢

2 个答案:

答案 0 :(得分:1)

我得到了解决方案Geetha,我为DataGridView处理了EditingControlShowing事件。 代码如下:

private void Lot_dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control is DataGridViewTextBoxEditingControl)
    {
        if (ColIndex == "2") // this colIndex i got it from CellEnter event.
        {
            DataGridViewTextBoxEditingControl te = (DataGridViewTextBoxEditingControl)e.Control;
            te.TextChanged += new EventHandler(textbox_TextChanged);
        }
    }
}

然后我处理了textbox_TextChanged事件。

void textbox_TextChanged(object sender, EventArgs e)
{
    TextBox tb = (TextBox)sender;
    MessageBox.Show(tb.Text);
    // Do your changes here.
    // To Change focus from the current cell use
    SendKeys.Send("{TAB}"); // to give focus to next cell in the same row.
}

答案 1 :(得分:0)