Fire and Event from another Event within the same form

I have a backgroundworker in my form.  Within the RunCompleted event, I want to call a CellPainting Event.  How do I do this?
MichelleLacyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RishadanPortCommented:
Have you tried just simply calling the function?

CellPainting_Function(this, new EventArgs());

?
0
MichelleLacyAuthor Commented:
I tried it and I got the following message
 
Error 2 'System.EventArgs' is a 'type' but is used like a 'variable'
0
RishadanPortCommented:
What is the declaration of the function CellPainting Event
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

MichelleLacyAuthor Commented:
I also tried
but got the following message:
datagridview1_CellPainting(this, e);
Error 3 Argument '2': cannot convert from 'System.ComponentModel.RunWorkerCompletedEventArgs' to 'System.Windows.Forms.DataGridViewCellPaintingEventArgs'
 
 
0
RishadanPortCommented:
ok so your declaration is then this:

datagridview1_CellPainting(object, System.Windows.Forms.DataGridViewCellPaintingEventArgs)
0
MichelleLacyAuthor Commented:
Cell painting event below:
private void dvgHomologs_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            try
            {
                int index = datagridview1.RowCount;
 
                for (int i = 1; i < index; i++)
                {
                    DataGridViewCell cell = datagridview1[3, i];
                    if (!string.IsNullOrEmpty(cell.Value.ToString()))
                    {
                        if ((cell.Value.ToString() == "car") && (e.Value.ToString() == cell.Value.ToString()))
                        {
                            e.CellStyle.BackColor = Color.Green;
                        }
                        else if ((cell.Value.ToString() == "truck") && (e.Value.ToString() == cell.Value.ToString()))
                        {
                            e.CellStyle.BackColor = Color.Orange;
                        }
                    }
                }
            }
            catch (System.NullReferenceException ex) 
            {
                MessageBox.Show(ex.ToString());
            }
        }

Open in new window

0
Toms EdisonTechnical ManagerCommented:
Error 3 Argument '2': cannot convert from 'System.ComponentModel.Run
WorkerCompletedEventArgs' to 'System.Windows.Forms.DataGridViewCellPaintingEventArgs'
 
This error is occurring because you are trying to pass the wrong argument. You should be createing a new DataGridViewCellPaintingEventArgs object and pass it to the event handler call
0
RishadanPortCommented:
You can't create the DataGridViewCellPaintingEventArgs, instead, have you tried simply indirectly calling the function, by telling the data gridview to repaint itself?
0
MichelleLacyAuthor Commented:
oh, I forgot to mention this event is being fired from another event, so perhaps I should be using this second event..
 

private void datagridview1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
this.datagridview1.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.datagridview1_CellPainting);
}
0
RishadanPortCommented:
Why exactly are you even trying to call this Cell Painting function directly? What are you trying to achieve?

private void datagridview1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
   this.dataGridView.Refresh();   <-- this will update the dataGridView and indirectly call that function
}
0
MichelleLacyAuthor Commented:
I added
this.datagridview1.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(this.ddatagridview1._DataBindingComplete);
to
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) and the application did not crash, but it did not paint any cells either...
0
MichelleLacyAuthor Commented:
so I am populated a datagridveiw with information from a database.  once the datagridview is populated, I want to color cells certain colors based on their content.  So if a cell in column x, contains car color it green; if it contains truck color it orange.
I had this working fine until, but when I added the background worker it no longer works.  
 
0
RishadanPortCommented:
In your form constructor do something like this:

public Form(){
   ...
   this.datagridview1.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(dvgHomologs_CellPainting);
   this.datagridview1.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(datagridview1._DataBindingComplete);
}
 
//DataBindingComplete Function
private void datagridview1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 
{ 
   this.dataGridView.Refresh();   <-- this will update the dataGridView and indirectly call that function
} 
 
//cell painting function
public void dvgHomologs_CellPainting(...){
}

Open in new window

0
RishadanPortCommented:
First try this, if this still crashes, that means that the thing making the refresh call is not on the UI thread.
0
MichelleLacyAuthor Commented:
it did crash again.  How do I get it on the correct thread?
0
MichelleLacyAuthor Commented:
is it actually the thread the backgroundworker is on?
0
RishadanPortCommented:
try this
//declare this anywhere outside the function
public void delegate MyDelegate();
 
private void datagridview1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 
{ 
   this.Invoke(new MyDelegate(delegate(){
      this.dataGridView.Refresh();
   }));
} 

Open in new window

0
MichelleLacyAuthor Commented:
I entered it.  I removed the code from the constructor, cells not painted.
if i put the cell_painting event anywhere on the form it crashes

this.datagridview1.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.datagridview1_CellPainting);
 
 
0
RishadanPortCommented:
ok... You have to show me the full code, and I will show you the problems.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MichelleLacyAuthor Commented:
thanks for the effort, but I am unable to send you my full code.  My solution was to take out the background worker.  Thanks for your effot.
0
RishadanPortCommented:
np, have a good day
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.