?
Solved

Data Grid View Validating Event

Posted on 2010-08-12
5
Medium Priority
?
685 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 2000 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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

743 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