how to validate Gridview cell's text .
-
HI, I have a grid control which has three columns viz.quantity, unit price, and total Amount column. Quantity column can have only integer value. I want to validate quantity column in a way so that as soon as user insert value other than integer, a message box should display showing error and return the focus to the cell. OR System should not allow any value other than integer into the cell. Is there any keypress/keydown/keyup event for datagrid's cell also? If yes than please provide me code or any hint... Please help.
Arun Singh Noida.
-
HI, I have a grid control which has three columns viz.quantity, unit price, and total Amount column. Quantity column can have only integer value. I want to validate quantity column in a way so that as soon as user insert value other than integer, a message box should display showing error and return the focus to the cell. OR System should not allow any value other than integer into the cell. Is there any keypress/keydown/keyup event for datagrid's cell also? If yes than please provide me code or any hint... Please help.
Arun Singh Noida.
You could try the following: 1 - Handle the DataGridView.EditingControlShowing event
private void DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
// Make sure we're getting the key press event for a text type editor...
if (e.Control.GetType() == typeof(DataGridViewTextBoxEditingControl))
// Handle the editing control's keypress event...
(e.Control).KeyPress += DataGridViewTextBoxEditingControlTextBoxControl_KeyPress;
}2 - Inside the keypress event, check for the keys you need and process them
private void DataGridViewTextBoxEditingControlTextBoxControl_KeyPress(object sender, KeyPressEventArgs e)
{
// Standard editing keys can be processed...
if (e.KeyChar == (char)Keys.Back)
{
e.Handled = false;
// No need for further validation...
return;
}// Just evaluate the contents of the cell if the value type is int
// NOTE: _dataGridView here is a reference to the grid in context...
if (_dataGridView.CurrentCell.ValueType != typeof (int))
return;// Validate the key char...
// NOTE: By specifying e.Handled = true, we are indicating that
// we absorbed the character and this key is not sent to the cell.
e.Handled = char.IsNumber(e.KeyChar);
}Hope this helps...
/F - .NET Developer
modified on Wednesday, July 30, 2008 11:55 AM
-
You could try the following: 1 - Handle the DataGridView.EditingControlShowing event
private void DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
// Make sure we're getting the key press event for a text type editor...
if (e.Control.GetType() == typeof(DataGridViewTextBoxEditingControl))
// Handle the editing control's keypress event...
(e.Control).KeyPress += DataGridViewTextBoxEditingControlTextBoxControl_KeyPress;
}2 - Inside the keypress event, check for the keys you need and process them
private void DataGridViewTextBoxEditingControlTextBoxControl_KeyPress(object sender, KeyPressEventArgs e)
{
// Standard editing keys can be processed...
if (e.KeyChar == (char)Keys.Back)
{
e.Handled = false;
// No need for further validation...
return;
}// Just evaluate the contents of the cell if the value type is int
// NOTE: _dataGridView here is a reference to the grid in context...
if (_dataGridView.CurrentCell.ValueType != typeof (int))
return;// Validate the key char...
// NOTE: By specifying e.Handled = true, we are indicating that
// we absorbed the character and this key is not sent to the cell.
e.Handled = char.IsNumber(e.KeyChar);
}Hope this helps...
/F - .NET Developer
modified on Wednesday, July 30, 2008 11:55 AM
Thanx
Arun Singh INDIA-Noida. My limit is sky and sky is too high.