Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 8808 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > Windows Forms > Validation of DataGridView Cell in Windows Application. ...
Santosh4u

Validation of DataGridView Cell in Windows Application.

 Code Snippet posted by: Santosh4u | Posted on: 10/25/2009 | Category: Windows Forms Codes | Views: 4225 | Status: [Member] | Alert Moderator   


Description:
In Web Application Validation is little bit easy,but in Windows Application it is little bit Difficult to validate the Controls,here i am giving the Very Nice Code to Validate the DataGridView Cells.

i am Implemented Same code in My Project for Validating Cells.
Let you want to allow only digits in DataGridView Cell then Below code will very helpful.

VB.NET
'dgv (DataGridView Name)
Private Sub dgv_EditingControlShowing(ByVal sender As System.Object, ByVal e As 

System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgv.EditingControlShowing
AddHandler e.Control.KeyPress, AddressOf CellValidation
End Sub
Sub CellValidation(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim strval As String = dgv.CurrentCell.EditedFormattedValue.ToString
If strval.Length <= 6 Then
Select Case Asc(e.KeyChar)
Case AscW(ControlChars.Cr) 'Enter key
e.Handled = True
Case AscW(ControlChars.Back) 'Backspace
Case 48 To 57 'Negative sign, Decimal and Numbers
Case Else ' Everything else
e.Handled = True
End Select
End If
If strval.Length >= 6 Then
Select Case Asc(e.KeyChar)
Case AscW(ControlChars.Back) 'Backspace
Case 48 To 57 'Negative sign, Decimal and Numbers
If strval.Length >= 3 Then
strval = ""
e.Handled = True
End If
Case Else ' Everything else
e.Handled = True
End Select
Exit Sub
End If
End Sub

C#.NET
 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{
e.Control.KeyPress += CellValidation;
}
public void CellValidation(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
string strval = dgv.CurrentCell.EditedFormattedValue.ToString();
if (strval.Length <= 6)
{
if (System.Convert.ToInt32(e.KeyChar) == System.Convert.ToInt32('\r')) //Enter key
{
e.Handled = true;
}
else if (System.Convert.ToInt32(e.KeyChar) == System.Convert.ToInt32('\b')) //Backspace
{
}
else if (System.Convert.ToInt32(e.KeyChar) >= 48 && System.Convert.ToInt32(e.KeyChar) <= 57)
//Negative sign, Decimal and Numbers
{
}
else
{
e.Handled = true;
}
}
if (strval.Length >= 6)
{
if (System.Convert.ToInt32(e.KeyChar) == System.Convert.ToInt32('\b')) //Backspace
{
}
else if (System.Convert.ToInt32(e.KeyChar) >= 48 && System.Convert.ToInt32(e.KeyChar) <= 57)
//Negative sign, Decimal and Numbers
{
if (strval.Length >= 3)
{
strval = "";
e.Handled = true;
}
}
else
{
e.Handled = true;
}
return;
}
}


Regards
Santosh
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/20/2013 2:28:32 PM