Link to home
Start Free TrialLog in
Avatar of San24
San24

asked on

Data Grid View Validating Event

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?
Avatar of Nash2334
Nash2334

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?
Avatar of San24

ASKER

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.
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();
}
Avatar of San24

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Nash2334
Nash2334

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial