milicica


I need prevent emergence signs from numeric keypad in cells of DataGridView and rerout their entry at textBox1.
This code is solve the task in textbox controls, but at DataGridView no prevent entry at cell, nor from them does pass-off.
Does anybody know how to solve this

private void dataGridViewOrders_KeyDown (object sender, KeyEventArgs e)
{

if ((e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) ||
e.KeyCode == Keys.Add || e.KeyCode == Keys.Divide || e.KeyCode == Keys.Multiply || e.KeyCode == Keys.Subtract || e.KeyCode == Keys.Decimal)
{
// Determine whether the keystroke is a backspace.
if (e.KeyCode != Keys.Back)
{

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


private void dataGridViewOrders_KeyPress(object sender, KeyPressEventArgs e)

{
// Check for the flag being set in the KeyDown event.
if flag == true)
{
textBox1.Text = Convert.ToString(e.KeyChar);
// Stop the character from being entered into the control since it is from the numeric keypad.
e.Handled = true;
flag = false;
}

}



Re: Visual C# General DataGridView - KeyDown and KeyPress events

Figo Fei - MSFT

Hi, milicica

Get the DataGridViewTextBoxEditingControl (derived from textbox control, looks like a textbox) in the DataGridView.EditingControlShowing event.

Add a KeyPress event for it and do it the same way as the normal textbox.

Here is the code sample:

private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

        {

            TextBox innerTextBox;

            if (e.Control is TextBox)

            {

                innerTextBox = e.Control as TextBox;

                innerTextBox.KeyPress += new KeyPressEventHandler(innerTextBox_KeyPress);

            }

        }

private void innerTextBox_KeyPress(object sender, KeyPressEventArgs e)

        {

            if (e.KeyChar >= '0' && e.KeyChar <= '9')

            {

                e.Handled = true;

            }

        }

 

If you have further problems, please feel free to let me know. Thank you






Re: Visual C# General DataGridView - KeyDown and KeyPress events

milicica


It's works. Thank you very much Figo Fei. But I still have a problem. I can't suppress Enter in cells of DGV and replace that event with my code - on enter event selection go to next row DGV and ignore this part of code *.

bool flag=false;

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox innerTextBox;

if (e.Control is TextBox)
{
innerTextBox = e.Control as TextBox;
innerTextBox.KeyPress += new KeyPressEventHandler(innerTextBox_KeyPress);
innerTextBox.KeyDown += new KeyEventHandler(innerTextBox_KeyDown);
}
}

private void innerTextBox_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) || e.KeyCode == Keys.Add || e.KeyCode == Keys.Divide || e.KeyCode == Keys.Multiply || e.KeyCode == Keys.Subtract || e.KeyCode == Keys.Decimal)
{ flag = true;}

* if (e.KeyCode = Keys.Enter)
*        { //some code instead enter; flag = true; }

}

private void innerTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (flag == true)
{
textBox1.Text = Convert.ToString(e.KeyChar);
e.Handled = true;
flag = false;
}
}





Re: Visual C# General DataGridView - KeyDown and KeyPress events

Figo Fei - MSFT

Hi, milicica

You may not do that with normal datagridview, because when pressing enter in innerTextBox (actually DataGridViewTextBoxEditingControl), it will fire some other method in datagridview and interrupt the innerTextBox's keydown event. (so you can't make it to execute the code in keydown event)

So, if you really want to realize it, you'd implement another customized datagridview control derived from normal one for yourself use. Override that method, making the datagridview.CurrentCell not changed.

That would be my idea. Thank you






Re: Visual C# General DataGridView - KeyDown and KeyPress events

Figo Fei - MSFT

Hi,

I found the "methods".

They are ProcessDialogKey(Keys keyData) and ProcessDataGridViewKey(KeyEventArgs e).

Then, you can make a custom DataGridView derived from System.Windows.Forms.DataGridView, and override these two methods to avoid such a behavior.

Thank you.






Re: Visual C# General DataGridView - KeyDown and KeyPress events

Figo Fei - MSFT

I've tried and made it.

Here is the custom DataGridView code:

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

 

    public class MyDataGridView:DataGridView

    {

        protected override bool ProcessDialogKey(Keys keyData)

        {

            if (keyData == Keys.Enter)

            {

                DataGridViewCell cell = CurrentCell;

                base.ProcessDialogKey(keyData);

                CurrentCell = cell;

                return true;

            }

            return base.ProcessDialogKey(keyData);

        }

        protected override bool ProcessDataGridViewKey(KeyEventArgs e)

        {

            if (e.KeyCode == Keys.Enter)

                return true;

            return base.ProcessDataGridViewKey(e);

        }

    }

 

Thank you

 






Re: Visual C# General DataGridView - KeyDown and KeyPress events

milicica

Thanks a Lot, Figo Fei. These posts were helpful.



Re: Visual C# General DataGridView - KeyDown and KeyPress events

Intraser


Thank for your post.