Link to home
Start Free TrialLog in
Avatar of MichelleLacy
MichelleLacy

asked on

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?
Avatar of RishadanPort
RishadanPort

Have you tried just simply calling the function?

CellPainting_Function(this, new EventArgs());

?
Avatar of MichelleLacy

ASKER

I tried it and I got the following message
 
Error 2 'System.EventArgs' is a 'type' but is used like a 'variable'
What is the declaration of the function CellPainting Event
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'
 
 
ok so your declaration is then this:

datagridview1_CellPainting(object, System.Windows.Forms.DataGridViewCellPaintingEventArgs)
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

Avatar of Toms Edison
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
You can't create the DataGridViewCellPaintingEventArgs, instead, have you tried simply indirectly calling the function, by telling the data gridview to repaint itself?
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);
}
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
}
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...
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.  
 
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

First try this, if this still crashes, that means that the thing making the refresh call is not on the UI thread.
it did crash again.  How do I get it on the correct thread?
is it actually the thread the backgroundworker is on?
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

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);
 
 
ASKER CERTIFIED SOLUTION
Avatar of RishadanPort
RishadanPort

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
np, have a good day