Click here to Skip to main content
15,898,924 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i use cell_painting event to paint group of cells(3 cells in the same row) in my dataGridView.
is it possible to DrawString in the middle of the painted cells ?

i tried use RectangleF but the String Appeared more then one time.

i have List of points that i keep there the painted cells, when i drag item from listbox to the DataGridView i want that in the middle of the painted cells it will be writen something for example "hello to everybody" but this string Appeares Three times, one on the another


C#
private List<Point> GeneralCourseWithHours = 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;
                GeneralCourseWithHours.Add(new Point(info.RowIndex, info.ColumnIndex));
                GeneralCourseWithHours.Add(new Point(info.RowIndex, info.ColumnIndex + 1));
                GeneralCourseWithHours.Add(new Point(info.RowIndex, info.ColumnIndex + 2));
            }
     }



C#
protected void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (GeneralCourseWithHours.Contains(new Point(e.RowIndex, e.ColumnIndex)))
            {
                e.Graphics.FillRectangle(Brushes.LightBlue, e.CellBounds);
                e.PaintContent(e.CellBounds);
                e.Handled = true;
                dataGridView1.Invalidate();

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

                str = "hello to everybody";

                rectDest = new RectangleF(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Size.Width * 3, e.CellBounds.Height);
                e.Graphics.DrawString(str, new Font("Arial", 10, FontStyle.Regular), Brushes.Black, rectDest, sf);
            }
        }
Posted
Updated 2-May-12 1:50am
v4
Comments
Sergey Alexandrovich Kryukov 1-May-12 20:23pm    
More then one time? Could you make a complete code sample, very small one?
--SA
BobJanova 2-May-12 8:51am    
What you're doing looks close to right, though you might want to set a clipping rectangle.

1 solution

The CellPainting method is called for every single cell that is to be (re-)painted. Therefore e.CellBounds returns the bounding rectangle of the cell that is currently painted.
As you have 3 cells in your list, you tell to draw it 3 times.

Try removing the 2nd and 3rd Point from the list:

GeneralCourseWithHours.Add(new Point(info.RowIndex, info.ColumnIndex));
GeneralCourseWithHours.Add(new Point(info.RowIndex, info.ColumnIndex + 1));
GeneralCourseWithHours.Add(new Point(info.RowIndex, info.ColumnIndex + 2));


You are specifying the width (3 cells) anyway in the CellPainting-method:

rectDest = new RectangleF(e.CellBounds.X, e.CellBounds.Y,
                          e.CellBounds.Size.Width * 3, e.CellBounds.Height);



Hope that helps!
 
Share this answer
 

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