Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi all,

I have extended the DataGridView to have more features than the default control. I have added a feature that shows a PictureBox with an icon if a cell cannot display all it's contents. If the icon is clicked it changes the wrap-mode of the owning row and changes the AutoSizeRowsMode to displayed-cells.

I'm showing this icon by listening to the CellMouseEnter event and then calculating the rectangle to place the icon in the bottom right corner of the cell as follows:

if ((e.RowIndex < 0) || (e.ColumnIndex < 0))
         {   
            //_peExpand is a normal PictureBox         
            _pbExpand.Visible = false;
            return;
         }
         DataGridViewCell CellToUse = this[e.ColumnIndex, e.RowIndex];
         if (CellToUse.Size.Width < CellToUse.PreferredSize.Width)
         {
            Rectangle DisplayRect = this.GetCellDisplayRectangle(e.ColumnIndex, 
            e.RowIndex, false);            
            _pbExpand.Location = new Point(DisplayRect.Right -   
            _pbExpand.Image.Width, DisplayRect.Bottom - _pbExpand.Image.Height);            
            _pbExpand.Tag = CellToUse;
            _pbExpand.Visible = true;
            _pbExpand.Image = (CellToUse.OwningRow.DefaultCellStyle.WrapMode == 
            DataGridViewTriState.True) ? Resources.chevron_icon :   
            Resources.chevron_expand_icon;            
         }



My problem comes after the row has been expanded and the mouse pointer leaves the cell and enters again later. It ignores the PictureBox (it's still drawn to the correct place). If I hold the mouse pointer over the PictureBox then it fires the CellMouseEnter event hundreds of times instead of firing the MouseEnter event for the PictureBox. It works perfectly fine if the row is expanded and the pointer never leaves the cell. If anyone can point me in the right direction I would appreciate it.

Edit: It only seems to happen when the DataGridView property AutoSizeRowsMode is set to DisplayedCells. It also seems to happen only when there is a control on the cell & the mouse pointer enters that control.
Posted
Updated 10-May-11 2:20am
v2
Comments
BobJanova 10-May-11 9:56am    
Glad you fixed it. The DataGridView is rather temperamental if you start trying to do anything clever with it.
XtErMiNaToR102 10-May-11 9:59am    
Yup, it creeps up on you when you least expect it

1 solution

Found it!!

Because I'm setting the PictureBox to visible = true in the CellMouseEnter event I needed to set it to false in the CellMouseLeave event. The problem is caused by setting the PictureBox to visible = false. I commented out that bit and it resolved the issue.
 
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