Solved

Data Grid View Validating Event

Posted on 2010-08-12
5
654 Views
Last Modified: 2013-12-17
Experts,

How do I trigger the Data Grid View Validating event from an external event like a Button Press?

I tried doing something like this. But doesn`t work.

 private void TestBut_Click(object sender, EventArgs e)
        {
           GridView.CellValidating +=new DataGridViewCellValidatingEventHandle (GridView_CellValidating);
        }

private void GridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
              //Validation here
        }

What if I want to trigger the Validating events from a different form?
0
Comment
Question by:San24
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:Nash2334
ID: 33421091
The validating event fires on the UI when a cell loses focus, so you can't fire it from another form.  What are you trying to do exactly?
0
 

Author Comment

by:San24
ID: 33421557
I have a group of custom user control which contain DataGridView. I want to Validate the cells when the user clicks a Button outside the User Control. Look if there are errors present in the User Controls, and based on that carry on the next steps.

Example : UserCntrl1, UserCntrl2, .... are on the main form. When I click on the Save Menu item  on the main form, I want the contents to be validated and then saved to a file.
0
 
LVL 10

Expert Comment

by:Nash2334
ID: 33422084
You can create a public method in the user control and invoke it from the main form in this case.

Example:
---------------------------------
UserControl

public bool ValidateAndSave()
{
bool _isValid = false;
...
if (_isValid)
  SaveToFile();

return _isValid;
}
---------------------------------
Main Form

public void button1_Click(object sender, EventArgs e)
{
_myUserControl.ValidateAndSave();
}
0
 

Author Comment

by:San24
ID: 33422627
So, create a separate function to Validate the DataGridView and not use the Validating or Validated events?

Example :  

private void ValidatetMe(DataGridView MotGridView, DataGridViewCell e)
 instead of
private void MotGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)

and then call the ValidateMe method from a different event?
0
 
LVL 10

Accepted Solution

by:
Nash2334 earned 500 total points
ID: 33422973
Yes.  Don't be confused about the name of the event "Validating".  CellValidating fires on the datagridview when a cell loses input focus and does nothing more.

If you are "validating" data, e.g. making sure that the data is clean, and you want to do that from an external control, you will need to expose a public (or protected) method that does what you want from the control itself and invoke it from the external event.

Also, your own ValidateMe method does not need to have a datagridview or cell passed in as a parameter - you will have access to all of those controls from your method.  ValidateMe() will be fine.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using C#, how could you improve upon my method of writing out a merged, compressed file? 2 34
Connection String 16 43
SQL Exceptions 3 37
Coding C# in Linux 8 24
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now