Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Cannot implicitly convert type
 

Cannot implicitly convert type

I am using my own class for "DataGridViewCellPaintingEventArgs" which I obtained from here:
http://www.koders.com/csharp/fid1DBCB1E079D023FE239DE7F27C34FAC67DA643AF.aspx?s=cdef%3Adatagridview#L34

When I incorporate it into my project by adding this handler:
dgv1.CellPainting += new DataGridViewCellPaintingEventHandler(dgv1_CellPainting);

I get this error on the handler (dgv1_CellPainting) :
Cannot implicitly convert type '****.Form1.DataGridViewCellPaintingEventHandler' to 'System.Windows.Forms.DataGridViewCellPaintingEventHandler'

I am using this line as the delegate:
public delegate void DataGridViewCellPaintingEventHandler(object sender, DataGridViewCellPaintingEventArgs e);

If I remove the delegate I get this error:
No overload for 'dgv1_CellPainting' matches delegate 'System.Windows.Forms.DataGridViewCellPaintingEventHandler'

I'm really not sure what I should do to correct this issue!
I want to use this class so I can modify (set) some of its properties (mainly ClipBounds) as all of them are read only!

Any help appreciated.

john....

john1166  Friday, September 25, 2009 6:13 AM
I don't understand why you are providing your own class. What aren't you using this one? The problem is that the DataGridView's CellPainting event uses the delegate defined in the System.Windows.Forms namespace, which in turn use the event args I linked to above.
dekurver  Friday, September 25, 2009 10:42 PM
Al, thank you for taking the time to answer my question.

Ok, well I should have explained a little better what I am trying to do in my last post above!

My issue is not with the CellPainting handler itself but with the DataGridViewCellPaintingEventArgs passed in when it is fired. If I provide my own class, this overrides the one in the System.Windows.Forms.dll allowing me to set some of the properties so I can modify for example the ClipBounds property. My reason for doing this is to improve efficiency of the DataGridView. With a little experimenting, I have discovered that the ClipBounds property determines the area that needs to be repainted. If only one cell changes then ClipBounds will reflect a value pertaining to that one cell. On the other hand, if 2 or more cells change value, then ClipBounds will be set to an area covering those cells and everything in between! As you can imagine, on a large grid, all those cells in between (of whose values have not changed) means a lot of unnecessary repainting severely degrading the performance of the DataGridView!

I hope you understand what it is I am trying to do here. If not, I can post a simple example I tested in code to demonstrate it.
I am merely looking for a way to improve the performance of the DataGridView as I use them extensively in my applications. Some of which can take up the entire screen.

Looking forward to your response

john.....
john1166  Saturday, September 26, 2009 6:54 AM
You have to provide a signature that is compatible with the event handler signature that the CellPainting event defines. This means the event args parameter must be the Windows Forms DataGridViewCellPaintingEventArgs or one of its base classes (HandledEventArgs or EventArgs). If you want to use your own type then the only thing I can suggest is that you override the DataGridView's OnCellPainting method to raise another event that can accept your custom event args.
dekurver  Saturday, September 26, 2009 4:22 PM
Sorry dekurver, I called you Al in my last post! Oops!

Once again, the class I'm using I didn't put together myself I obtained it from here: It appears to be using the base class (HandledEventArgs)
http://www.koders.com/csharp/fid1DBCB1E079D023FE239DE7F27C34FAC67DA643AF.aspx?s=cdef%3Adatagridview#L34

I'm really not sure what you mean by providing a compatible signature. Do you mean return type and argument types passed by the handler? Does that include name spaces? I'm still very new to Object Oriented Programming!

From what I can tell, the class I want to use for the DataGridViewCellPaintingEventArgs is identical to the one internal to the System.Windows.Forms.dll because I also get this warning message wherever I use DataGridViewCellPaintingEventArgs:

The type 'System.Windows.Forms.DataGridViewCellPaintingEventArgs' in '...\DataGridViewCellPaintingEventArgs.cs' conflicts with the imported type 'System.Windows.Forms.DataGridViewCellPaintingEventArgs' in 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll'. Using the one in '...\DataGridViewCellPaintingEventArgs.cs'.

If I don't add the handler and the delegate and still use the file 'DataGridViewCellPaintingEventArgs.cs' in my project, I can place 'set { ClipBounds = value; }' in the DataGridViewCellPaintingEventArgs.cs file and be able to get or set that property. I still get the warning messages but the program runs. Basically all I want to do is create my own DataGridViewCellPaintingEventArgs object, initialize it, modify some (not all) of its properties and call the CellPainting Event myself passing in my own arguments. But I don't know if that's possible yet!

john.....
john1166  Saturday, September 26, 2009 5:52 PM
I think I understand what you're attempting but I don't think you can do it the way you're doing it. You'll need to create a custom DataGridView, override the OnCellPainting event and provide a new instance of the DataGridViewCellPaintingEventArgs to pass to the base class. E.g.

public class MyDataGridView : DataGridView
{
    protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
    {
        // Initialize a new event args.
        var newArgs = new DataGridViewCellPaintingEventArgs(...);

        // Pass the new event args to the base class, which will 
        // raise the event.
        base.OnCellPainting(newArgs);
    }
}
dekurver  Sunday, September 27, 2009 11:23 PM
Ok well I think I know why I have to provide my own OnCellPainting event! Is it because this will fire the CellPainting event? And if so, in the example you have provided, does this mean newArgs is initialized every time the OnCellPainting event is triggered? I would prefer to use a variable (perhaps global) that only needs some of its properties modified before being passed on as this would be more efficient (faster) especially with large amounts of cells being updated.

I have been successful in creating a DataGridViewCellPaintingEventArgs and initializing all property values with no problems, however without using the DataGridViewCellPaintingEventArgs.cs file and changing some properties to (get & set), all properties remain read only! So I'm back to my original problem of " Cannot implicitly convert type".

I haven't done enough testing yet but will this solve the issue associated with the ClipBounds property setting the area that will be repainted as I have outlined above?

john....

john1166  Tuesday, September 29, 2009 11:48 AM
To be honest, I'm not convinced you can control the redraw sufficiently with the CellPainting event to achieve what you want. It is likely that the control background has already been repainted before this event fires.This means if you prevent some cells from redrawing - even if you believe they are being redrawn unnecessarily - those cells could appear blank since their backgrounds have already been repainted. I suspect the ClipBounds property used in the event args is simply derived from theupdate regionof the control whenit receives aWM_PAINT message. Since this is a rectangle, it can only describe the area that is the union of thecells that need to be updated.

Are you certain your performance problems are the result of unnecessary painting in the control? Have you profiled your application? Can you provide more details about the symptoms of your performance problems?
dekurver  Tuesday, September 29, 2009 4:26 PM
I'm pretty sure the poor performance is related to unnecessary repainting of the cells.

Rather than me explain here, have a look at this thread I started on the 16th September:
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/8e7ff594-bbf4-4abd-b494-796baf7f52d1
On my 2nd post of that thread, I posted a very simple app to demonstrate how many times the CellPainting event is fired just for changing the current cell.

Please try it out!

john...

john1166  Tuesday, September 29, 2009 7:28 PM

You can use google to search for other answers

Custom Search

More Threads

• Howto set different colors for different databound items in datagrid??
• data records from oracle, insert
• Create a dataview using criteria from a child table.
• databind to a listbox which allows multiple selection
• Creating Strongly Typed DataSets - Utalizing Existing Oracle Package
• multi column in table need add combobox
• Creating an INSERT query in DataSet Designer fails; bug?
• How to get the current Item "Data Error" in bindingSource?
• Using OVER Clause in TableAdapter Configuration Wizard
• DGV addingnew but no data