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 DataGridViewCellValidating EventHandl e (GridView_CellValidating);
}
private void GridView_CellValidating(ob ject sender, DataGridViewCellValidating EventArgs e)
{
//Validation here
}
What if I want to trigger the Validating events from a different form?
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 DataGridViewCellValidating
}
private void GridView_CellValidating(ob
{
//Validation here
}
What if I want to trigger the Validating events from a different form?
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?
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.
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.ValidateAnd Save();
}
Example:
--------------------------
UserControl
public bool ValidateAndSave()
{
bool _isValid = false;
...
if (_isValid)
SaveToFile();
return _isValid;
}
--------------------------
Main Form
public void button1_Click(object sender, EventArgs e)
{
_myUserControl.ValidateAnd
}
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, DataGridViewCellValidating EventArgs e)
and then call the ValidateMe method from a different event?
Example :
private void ValidatetMe(DataGridView MotGridView, DataGridViewCell e)
instead of
private void MotGridView_CellValidating
and then call the ValidateMe method from a different event?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.