.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Validation of DataGridView Cell in Windows Application.

Posted By:Santosh       Posted Date: November 04, 2009    Points: 10    Category: Windows Form Controls    URL: http://www.dotnetspark.com  
 

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;

}

}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend