Muhammad Mobeen Qureshi
Muhammad Mobeen Qureshi

Reputation: 1182

Numeric TextBox in DataGridView column

I have a DataGridView. I want its 1st column or any desired column (which has textboxes in it) to be NUMERIC ONLY. I am currently using this code:

private void dataGridViewItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns["itemID"].Index)
            {
                TextBox itemID = e.Control as TextBox;
                if (itemID != null)
                {
                    itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
                }
            }
        }

private void itemID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

This code works but the problem is that all the textboxes in all the columns are getting numeric only.

Upvotes: 2

Views: 5326

Answers (4)

XxELITExX
XxELITExX

Reputation: 1

Click the textbox that you want to be numeric only then in the properties click the "events" (looks like thunder) then look for the "keypress" then double click then type this:

if (char.IsNumber(e.KeyChar) || char.IsControl(e.KeyChar))
{
    e.Handled = false;
}
else
{
    e.Handled = true;
}

Upvotes: 0

Muhammad Mobeen Qureshi
Muhammad Mobeen Qureshi

Reputation: 1182

I figured it out myself :)

Just removed the previous events in the starting of the function which resolved my issue.

private void dataGridViewItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            e.Control.KeyPress -= new KeyPressEventHandler(itemID_KeyPress);//This line of code resolved my issue
            if (dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns["itemID"].Index)
            {
                TextBox itemID = e.Control as TextBox;
                if (itemID != null)
                {
                    itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
                }
            }
        }

private void itemID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

Upvotes: 2

spajce
spajce

Reputation: 7082

This is how to use EditingControlShowing with TextBoxKeypress Event

   private void dataGridViewItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        var itemID = e.Control as TextBox;
        if (dataGridViewItems.CurrentCell.ColumnIndex ==  1) //Where the ColumnIndex of your "itemID"
        {
            if (itemID != null)
            {
                itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
                itemID.KeyPress -= new KeyPressEventHandler(itemID_KeyPress);
            }
        }
    }

    private void itemID_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar))
            e.Handled = true;
    }

Upvotes: 0

Andres
Andres

Reputation: 2899

You can add in a List all the numeric cells index.

Like this:

List<int> list_numeric_columns = new List<int>{ dataGridViewItems.Columns["itemID"].Index};

You add all the columns you need there.

Then, instead of doing this:

if (dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns["itemID"].Index)

You do this:

if (list_numeric_columns.Contains(dataGridViewItems.CurrentCell.ColumnIndex))

That should work. You will have to add the columns just once..

Hope thats helps

Upvotes: 0

Related Questions