Check for illegal char in DataGridTextBoxColumn

W

Wild Gooose

Hi all, need some help in taking the right approach to
solve this problem.
I have a DataGrid bound to a DataTable. I need the user to
enter only numerical values and decimal. I did override
the ProcessCmdKey of DataGrid. I'm capturing Enter, Tab,
directinal and other keys. If the user enters an invalid
key, I need to prompt him. But, for this I'll need to map
all the other keys to be invalid in my code. Is there any
other slick way of doing this.

Code attached.

protected override bool ProcessCmdKey(ref Message msg,
Keys keyData)
{
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;
int rowNum = dGInput.CurrentCell.RowNumber;
int colNum = dGInput.CurrentCell.ColumnNumber;

if ((colNum == 2) || (colNum == 5))
{
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
switch(keyData)
{
case Keys.D0:
case Keys.D1:
case Keys.D2:
case Keys.D3:
case Keys.D4:
case Keys.D5:
case Keys.D6:
case Keys.D7:
case Keys.D8:
case Keys.D9:
case Keys.NumPad0:
case Keys.NumPad1:
case Keys.NumPad2:
case Keys.NumPad3:
case Keys.NumPad4:
case Keys.NumPad5:
case Keys.NumPad6:
case Keys.NumPad7:
case Keys.NumPad8:
case Keys.NumPad9:
case Keys.Decimal:
case Keys.OemPeriod:
break;
case Keys.Back:
break;
case Keys.Down:
case Keys.Up:
case Keys.Left:
case Keys.Right:
case Keys.Enter:
case Keys.Tab:
UpdateDataGridCell(rowNum,colNum);
break;
default:
MessageBox.Show("Invalid character","Error");
break;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top