Nipun
Nipun

Reputation: 1485

Limit the DataGridView column to 2 decimal place

Hi i have a gridview.My requirement is when the user enters a decimal value in a field, it should allow user to enter only 2 decimal place digits.After entering 2 decimal place the focus should get to next field. Thanks

Upvotes: 1

Views: 3087

Answers (2)

Nipun
Nipun

Reputation: 1485

I got the solution Geetha, I handled the event EditingControlShowing for my DataGridView. The code is below:

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

and then i handled the textbox_TextChanged event.

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.
}

Upvotes: 1

Related Questions