greggorob64
greggorob64

Reputation: 2557

Derived DataGridView drawing problem. Shows black regions

Late Edit I tagged this as a C# question as well as C++ because the problem presents itself in both languages, and solution if shown would most likely be in the C# (the majority of the market).

I have been developing an application under .net 2.0 (C++ to be specific, but irrelevant).

This application uses a custom derived datagridview. This datagridview will occasionally have sever artifacting issues regarding the region of the DGV that does not contain cells, as well as the scrollbar. During some resizing actions, a black rectangle will draw in the bottom portion of the datagridview, which will in effect limit the size of the grid. The scrollbar also gets shrunk to fit inside the nonbugged region. It seems to me as the system believes the DGV is the wrong size, and is drawing into the wrong region.

alt text http://img12.imageshack.us/img12/2213/81356991.jpg

There are only two ways I can find to fix the symptoms:
1. Clicking a column to resize will automatically fix the grid
2. Calling the AutoResizeRows() function in the DGV will do the fix (but I believe is something that is called from point 1).

Some of the modifications to the Custom DGV:
1) Configured to handle drag\drop of multiple rows.
2) Point 1 required OnCellPainting to be overridden to draw drag lines. Function can be posted if it seems symptomatic.
3) The problems always happen on a resize (both manual and automatic can cause the problem), but there is no custom code in the resize event.

late edit code for onCellPainting. Other functions overridden in the gridview: OnMouseDown, OnCellMouseDown, OnClick, OnMouseMove, OnDragOver, OnDragDrop, OnDragLeave, OnKeyDown, None of which seem symptomatic

   protected: [DebuggerStepThrough()]
   virtual System::Void OnCellPainting(DataGridViewCellPaintingEventArgs ^e) override 
   {
      //draws red drag/drop target indicator lines if necessary
      if (this->AllowDrop && _DragDropCurrentIndex > -1 && ShowDragLines)
      {
         System::Drawing::Pen ^p = gcnew Pen(System::Drawing::Color::Navy, 3);

         //row drag/drop
         if (e->RowIndex == _DragDropCurrentIndex && 
             _DragDropCurrentIndex <= this->RowCount)
         {
            //if this cell is in the same row as the mouse cursor
            e->Graphics->DrawLine(
               p, 
               e->CellBounds.Left, 
               e->CellBounds.Top - 1, 
               e->CellBounds.Right, 
               e->CellBounds.Top - 1);
         } //end if

         if(e->RowIndex == this->Rows->Count - 1 && 
            _DragDropCurrentIndex == Int32::MaxValue)
         {
            e->Graphics->DrawLine(
               p, 
               e->CellBounds.Left, 
               e->CellBounds.Bottom + 1, 
               e->CellBounds.Right, 
               e->CellBounds.Bottom + 1);            
         }
      } //end if
      DataGridView::OnCellPainting(e);
   } //end OnCellPainting

*More Edits None of these work to rid the problem, the only thing that fixes it AFTER the problem happens is AutoResizeRows(AllCells)// Only AllCells fixes it. It's very slow and undesirable.

Refresh(); UpdateBounds(); Update(); Invalidate(); PerformLayout(); ResetBackColor(); ResetBindings(); ResetForeColor(); ResetText(); UpdateStyles();

Upvotes: 4

Views: 6554

Answers (5)

PKanold
PKanold

Reputation: 148

I solved this by Maximizing the Form in the load event, populating the Datagridview, and then resetting the Form to the start size. After that, all resizing appears to work as expected. VB.Net code but you get the gist:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim StartSize As Size = Me.Size
    Me.Width = Integer.MaxValue
    Me.Height = Integer.MaxValue
    Me.Visible = False
' ... Populate and format the datagridview ...
    Me.Size = StartSize
    Me.Visible = True
End Sub

Upvotes: 0

Loathing
Loathing

Reputation: 5266

I had the same problem caused by some resx files not being loaded that were being referenced from the mDataGridView_CellPainting event. The DataGridView became very slow to resize as well.

Upvotes: 0

Sameer Jagdale
Sameer Jagdale

Reputation:

Try clearing the graphics with the grid's background in OnPaint:

Graphics g = e.Graphics;
g.Clear(this.BackColor);

Upvotes: 0

erik
erik

Reputation: 3880

It sounds to me like your control isn't rendering its layout properly.
Did you suspend layout at some point in your code, and then never resumelayout?

Doing so will allow your control to function properly, but not lay out all of its components properly.

Upvotes: 8

Andy
Andy

Reputation: 5268

Try setting the .ResizeRedraw property of your control to true in the constructor, see if that helps.

From MSDN:

Gets or sets a value indicating whether the control redraws itself when resized.

Upvotes: 0

Related Questions