Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
i use cell_painting event to draw Rectangle (in size of 3 cells). when i drag item from the listBox to the dataGridView the the Rectangle is blinking !!!
how i can fill Rectangle and draw string that it will not blink in the dataGridView ?

C#
private List<Point> GeneralCourses = new List<Point>();

C#
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
        {
            Point p = this.dataGridView1.PointToClient(new Point(e.X, e.Y));
            DataGridView.HitTestInfo info = this.dataGridView1.HitTest(p.X, p.Y);
            if (info.RowIndex != -1 && info.ColumnIndex != -1)
            {
                Object value = (Object)e.Data.GetData(typeof(string));

                this.dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value = value;
                GeneralCourses.Add(new Point(info.RowIndex, info.ColumnIndex));
             }
        }

C#
protected void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (GeneralCourses.Contains(new Point(e.RowIndex, e.ColumnIndex)))
            {
                e.Handled = true;
                dataGridView1.Invalidate();

                Rectangle rectDest = Rectangle.Empty;
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                sf.Trimming = StringTrimming.EllipsisCharacter;
                string str;

                Pen p = new Pen(Color.Blue);
                str = "string example";

 rectDest = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Size.Width * 3, e.CellBounds.Height);
                e.Graphics.DrawRectangle(p, rectDest);
                e.Graphics.FillRectangle(Brushes.LightBlue, rectDest);
                e.Graphics.DrawString(str, new Font("Arial", 10, FontStyle.Regular), Brushes.Black, rectDest, sf);
                p.Dispose();
                sf.Dispose();
            }
}
Posted
Updated 3-May-12 7:53am
v7

1 solution

Try taking the Invalidate call out of the CellPainting Event - it causes the DataGridView to repaint, which causes another CellPainting event, which Invalidates the DataGridView which...

If it still blinks when you have done that, try setting the FormDoubleBuffered property to True.
 
Share this answer
 
Comments
yehiad 3-May-12 14:16pm    
when i took out the Invalidate only one cell was painted (i have 3 cells that need paint - the Rectangle size is 3 cells size) and the Form DoubleBuffered to true didn't help.
OriginalGriff 3-May-12 14:26pm    
You can't paint three cells in a single cell CellPainting event - the supplied Graphics context is bounded to the cell borders to prevent you overrunning by accident.

What are you trying to achieve?
yehiad 3-May-12 14:45pm    
i use CellPainting event becaue of: e.RowIndex, e.ColumnIndex and that i can determine the position in the datagridview

i want to drag courses from listbox to datagridview (the Columns are hours, rows are days and courses i drag are more then one hour)

is it possible to do it with paint event?
OriginalGriff 3-May-12 15:18pm    
So you are trying to drag a course of (say) 3 hours onto a day and have it show as which three hours it would occupy?

Hmm. I don't honestly think I would use a DataGridView - I would prefer to create my own "Day" custom control and pile a bunch of them together to form the week. You are trying to twist a grid based object to do things it wasn't designed for, and that often creates more problems than it solves.
You might be able to do it with the Paint event - I've not tried - but I think it would be easier in the long run to roll your own.
yehiad 3-May-12 15:39pm    
i use SQL server database, i take from there the courses, this is way i use datagridview

thank's anyway

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