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?
ASKER
I tried it and I got the following message
Error 2 'System.EventArgs' is a 'type' but is used like a 'variable'
Error 2 'System.EventArgs' is a 'type' but is used like a 'variable'
What is the declaration of the function CellPainting Event
ASKER
I also tried
but got the following message:
datagridview1_CellPainting (this, e);
Error 3 Argument '2': cannot convert from 'System.ComponentModel.Run WorkerComp letedEvent Args' to 'System.Windows.Forms.Data GridViewCe llPainting EventArgs'
but got the following message:
datagridview1_CellPainting
Error 3 Argument '2': cannot convert from 'System.ComponentModel.Run
ok so your declaration is then this:
datagridview1_CellPainting (object, System.Windows.Forms.DataG ridViewCel lPaintingE ventArgs)
datagridview1_CellPainting
ASKER
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());
}
}
Error 3 Argument '2': cannot convert from 'System.ComponentModel.Run
WorkerCompletedEventArgs' to 'System.Windows.Forms.Data GridViewCe llPainting EventArgs'
This error is occurring because you are trying to pass the wrong argument. You should be createing a new DataGridViewCellPaintingEv entArgs object and pass it to the event handler call
WorkerCompletedEventArgs' to 'System.Windows.Forms.Data
This error is occurring because you are trying to pass the wrong argument. You should be createing a new DataGridViewCellPaintingEv
You can't create the DataGridViewCellPaintingEv entArgs, instead, have you tried simply indirectly calling the function, by telling the data gridview to repaint itself?
ASKER
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_DataBindingC omplete(ob ject sender, DataGridViewBindingComplet eEventArgs e)
{
this.datagridview1.CellPai nting += new System.Windows.Forms.DataG ridViewCel lPaintingE ventHandle r(this.dat agridview1 _CellPaint ing);
}
private void datagridview1_DataBindingC
{
this.datagridview1.CellPai
}
Why exactly are you even trying to call this Cell Painting function directly? What are you trying to achieve?
private void datagridview1_DataBindingC omplete(ob ject sender, DataGridViewBindingComplet eEventArgs e)
{
this.dataGridView.Refresh( ); <-- this will update the dataGridView and indirectly call that function
}
private void datagridview1_DataBindingC
{
this.dataGridView.Refresh(
}
ASKER
I added
this.datagridview1.DataBin dingComple te += new System.Windows.Forms.DataG ridViewBin dingComple teEventHan dler(this. ddatagridv iew1._Data BindingCom plete);
to
private void backgroundWorker1_RunWorke rCompleted (object sender, RunWorkerCompletedEventArg s e) and the application did not crash, but it did not paint any cells either...
this.datagridview1.DataBin
to
private void backgroundWorker1_RunWorke
ASKER
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.
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(...){
}
First try this, if this still crashes, that means that the thing making the refresh call is not on the UI thread.
ASKER
it did crash again. How do I get it on the correct thread?
ASKER
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();
}));
}
ASKER
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.CellPai nting += new System.Windows.Forms.DataG ridViewCel lPaintingE ventHandle r(this.dat agridview1 _CellPaint ing);
if i put the cell_painting event anywhere on the form it crashes
this.datagridview1.CellPai
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
CellPainting_Function(this
?