DataGridView TextBoxes

What is the easiest way to limit a textbox column in a datagridview to the entering of just numbers. I know how to do it on a regular textbox by using the keyup and keydown event handlers but am not sure how to do it on a datagridview textbox.

Also, I don't have a datasource and don't want one for the datagridview, I am using it for another purpose and for some reason it will not format the number to a currency even though I have that selected as the format under the default cell style. I have gotten this to work on another form in the app that has a datasource bound to the datagridview, why won't it work without a datasource bound to the grid

Casey




Answer this question

DataGridView TextBoxes

  • Nisheeth Tak

    Ok, so now I'm just getting irritated, when I have the DataGridView bound like I do on one of my other forms, I can type in "1" and when I press tab and/or leave the cell with the mouse it changes it to "$1.00". That is because I have the format specified as currency.

    Now, I have an unbound DataGridView and for the life of me can not get it to do the same thing



  • Brian Knox

    you are welcome

    but plz mark the answers for your question even if you answered yourself, but don't leave the thread like that

    best regards



  • rsl_t

    Ok, so I got it to format the number as a currency when it leaves the cell pretty much by doing the same thing as the above, I subscribed to a textbox leave event from the datagridview editingshowing event and it only works when I use the mouse to leave the cell. When, I use tab, enter, or any arrows, it does not format the text...



  • Richard Mabbitt

    Ok, so I figured out the way to limit text to characters only and have posted it below.. However, I still havn't figured out my formatting problem, although it is probably an easy fix with some event handling, I was hoping the datagrid view would do it to make it easier..

    The code is to restrict entry to currency entries only in a textbox found on a datagridview. It restricts you to only entering one period and any numbered character along with the backspace. It also restricts you from using the the shift key and entering characters such as %, ^, &... It will let you enter a period if there is a period in a selection...therefore eracing the first period when entering the new one (you would still only have one). This elminates the need for me to deal with invalid number errors.

    private void dataGridViewSplit_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

    {

    DataGridViewRow row = dataGridViewSplit.CurrentRow;

    DataGridViewCell cell1 = row.Cells[3];

    DataGridViewCell cell2 = row.Cells[4];

    //Unsubscribe from the event in case it is subscribed

    dataGridViewSplit.EditingControl.KeyPress -= new KeyPressEventHandler(editingControl_KeyPress);

    dataGridViewSplit.EditingControl.KeyDown -= new KeyEventHandler(editingControl_KeyDown);

    if (dataGridViewSplit.CurrentCell == cell1 || dataGridViewSplit.CurrentCell == cell2)

    {

    dataGridViewSplit.EditingControl.KeyPress += new KeyPressEventHandler(editingControl_KeyPress);

    dataGridViewSplit.EditingControl.KeyDown += new KeyEventHandler(editingControl_KeyDown);

    }

    }

    private void editingControl_KeyDown(object sender, KeyEventArgs e)

    {

    int selPos = 0;

    int regPos = 0;

    TextBox temp = (TextBox)sender;

    selPos = temp.SelectedText.IndexOf(".");

    regPos = temp.Text.IndexOf(".");

    // Initialize the flag to false.

    nonNumberEntered = false;

    if (regPos != -1 && (e.KeyCode == Keys.OemPeriod || e.KeyCode == Keys.Decimal))

    {

    if (selPos < 0)

    {

    nonNumberEntered = true;

    }

    }

    // Determine whether the keystroke is a number from the top of the keyboard.

    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9 || e.Modifiers != Keys.None)

    {

    // Determine whether the keystroke is a number from the keypad.

    if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9 || e.Modifiers != Keys.None)

    {

    // Determine whether the keystroke is a backspace or a period.

    if ((e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod && e.KeyCode != Keys.Decimal) || e.Modifiers != Keys.None)

    {

    // A non-numerical keystroke was pressed.

    // Set the flag to true and evaluate in KeyPress event.

    nonNumberEntered = true;

    }

    }

    }

    }

    private void editingControl_KeyPress(object sender, KeyPressEventArgs e)

    {

    // Check for the flag being set in the KeyDown event.

    if (nonNumberEntered == true)

    {

    // Stop the character from being entered into the control since it is non-numerical.

    e.Handled = true;

    }

    }



  • Craig Spargo

    that does help but I found it easier to redesign the form and bind a datasource to it. now it formats it go figure.. but thanks for the help, I think I am going to use what you have here to check for a valid number.



  • PParisot

    hi,

    i couldn't prevent the other values to be entered to this textbox column all what i could do is to show up an error sign here its


    private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
    {
    dataGridView1.EndEdit();
    if (e.ColumnIndex == 0)
    {
    double contents;
    bool correctvalue = double.TryParse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), out contents);
    if (correctvalue)
    {
    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = string.Format("{0:C2}", contents);
    }
    else

    {
    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText =
    "wrong entry";
    }
    }


    yes the datagridview is a great and irritating control it has lots of functions that will make you confused and most of them about formating the UI

    just one thing in your cellenter event handler you can remove the currancy formating by parsing if you like to

    hope this helps



  • DataGridView TextBoxes