• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 801
  • Last Modified:

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?
0
MichelleLacy
Asked:
MichelleLacy
  • 10
  • 10
1 Solution
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
 
TechTiger007Commented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 10
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now