DotNet Academy of Rajesh Rolen

Solutions by Rajesh Rolen

Restricting numeric entries only in a DataGridView column

At times we have a requirement where we need to restrict the user to enter only numbers in a column of a DataGridView to achieve this we need to use EditingControlShowing event of DataGridView, this event is new in .Net framework 2.0 and it occurs when a control for editing a cell is showing. Following sample shows a implementation of it:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if ((int)(((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex) == 1)
{
e.Control.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TextboxNumeric_KeyPress);

}
}

private void TextboxNumeric_KeyPress(object sender, KeyPressEventArgs e)
{
Boolean nonNumberEntered;

nonNumberEntered = true;

if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
{
nonNumberEntered = false ;
}

if (nonNumberEntered == true)
{
// Stop the character from being entered into the control since it is non-numerical.
e.Handled = true ;
}
else
{
e.Handled = false;
}

}

1 comments:

Anonymous December 18, 2009 at 6:03 PM  

Can anyone recommend the top performing Software Deployment system for a small IT service company like mine? Does anyone use Kaseya.com or GFI.com? How do they compare to these guys I found recently: N-able N-central desktop support
? What is your best take in cost vs performance among those three? I need a good advice please... Thanks in advance!

Post a Comment

About this blog

Blog Archive

My Blog List

Advertise On This Site

Site Info

Advertise on this Site

To advertise on this site please mail on RajeshRolen@gmail.com

Information Source

About

Pages

Dot Net Academy

Advertis in This Area of Site

Powered by Blogger.

Followers

Blog Archive

Search This Blog