[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Fire and Event from another Event within the same form

Posted on 2008-11-04
21
Medium Priority
?
789 Views
Last Modified: 2013-12-17
I have a backgroundworker in my form.  Within the RunCompleted event, I want to call a CellPainting Event.  How do I do this?
0
Comment
Question by:MichelleLacy
  • 10
  • 10
21 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22879132
Have you tried just simply calling the function?

CellPainting_Function(this, new EventArgs());

?
0
 

Author Comment

by:MichelleLacy
ID: 22879165
I tried it and I got the following message
 
Error 2 'System.EventArgs' is a 'type' but is used like a 'variable'
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22879175
What is the declaration of the function CellPainting Event
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:MichelleLacy
ID: 22879182
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
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22879193
ok so your declaration is then this:

datagridview1_CellPainting(object, System.Windows.Forms.DataGridViewCellPaintingEventArgs)
0
 

Author Comment

by:MichelleLacy
ID: 22879194
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
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22879212
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
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22879226
You can't create the DataGridViewCellPaintingEventArgs, instead, have you tried simply indirectly calling the function, by telling the data gridview to repaint itself?
0
 

Author Comment

by:MichelleLacy
ID: 22879236
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
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22879310
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
 

Author Comment

by:MichelleLacy
ID: 22879313
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
 

Author Comment

by:MichelleLacy
ID: 22879331
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
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22879422
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
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22879458
First try this, if this still crashes, that means that the thing making the refresh call is not on the UI thread.
0
 

Author Comment

by:MichelleLacy
ID: 22879489
it did crash again.  How do I get it on the correct thread?
0
 

Author Comment

by:MichelleLacy
ID: 22879558
is it actually the thread the backgroundworker is on?
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22879561
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
 

Author Comment

by:MichelleLacy
ID: 22879724
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
 
LVL 6

Accepted Solution

by:
RishadanPort earned 1500 total points
ID: 22879738
ok... You have to show me the full code, and I will show you the problems.
0
 

Author Closing Comment

by:MichelleLacy
ID: 31513214
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
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22898429
np, have a good day
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 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