Click here to Skip to main content
14,979,508 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i have datagridview that i use as a timetable and i also have listbox containing names of courses. i drag courses from listbox to datagridview. i use the paint event to paint courses in the datagridview and i also use the paint event (i paint the aera back to the color of the datagridview)when i move back course form the datagridview to the listbox.

it's work good. the problem when i move course in the datagridview and then i want to drag another course(from the listbox) to the same place in the datagridview, i can't see the paint event only if i will move it to another place in the datagridview and then back to the place i wanted just then it will paint the area i want.

there is a way to determine what event more powerful ? or another way to make sure that last event that occurs will be on top ???
Good question! I voted 5 for it.
I hope my explanations would be enough -- please see my answer.
If something's goes wrong, don't hesitate to ask a follow-up question. (If you do, to keep me notified, add a comment to my answer or any of my comments on this page.)

1 solution

There is no such thing as more or less powerful event. Your problem can be a bit tricky, but it's relatively easy to solve if you understand the idea. Rendering event is actually the Windows message WM_PAINT, which is very special, because Windows optimizes this part of messaging to avoid double re-painting. You can cause sending this message explicitly by calling Control.Invalidate. The Invalidate methods with a parameter (<core>Rectangle or Region help to do it with better performance be invalidating only a part of the scene.

Windows ORs (set-theory disjunction) all the invalidated regions and optimizes the messaging the way the union of the invalidated areas is re-painted at once, but without waiting in the message queue.

So, here is the idea for solving you problem. You need to handle some other events, the events changing selection and perhaps some others. You will see it when you develop; it also depends on how you render the control. In the event handlers, you would need to call Control.Invalidate for appropriate areas of your control. For example, if you change selection, you can use different selection mode. The affected area could be either a separate cell or entire row. So, you should invalidate the area which lost selection and immediately the area which got selection. Use this technique; and you will see that it solves your problem.

yehiad 9-May-12 16:26pm
I tried to Invalidate(Rectangle) in the paint event but it's not working.

What do you mean by "You need to handle some other events" ? i need to find another events that have an impact on the area of ​​rectangle ?
1) You could take a wrong Rectangle. Start with calling Invalidate(), without parameters. If it works and needs better performance, add the parameters to limit invalidation (and hence minimal re-painting) area;

2) You don't really have to find anything special. I mean, let's say, you change the selection and see that something is not re-painted. You can close you window with some other one, show it all again to see the difference. Now, see what was your action which causes no or insufficient invalidation. See what events are raised and handle it with invalidation. When it happens when you change selection, there are selection-change events. I might have forgotten about some other events which need invalidation. You will see them per action which is supposed to re-paint some part of the control.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900