Solved

Data Grid View Validating Event

Posted on 2010-08-12
5
650 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

12 Experts available now in Live!

Get 1:1 Help Now