Solved

Data Grid View Validating Event

Posted on 2010-08-12
5
671 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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