问题
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();
回答1:
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.
回答2:
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.
回答3:
Try clearing the graphics with the grid's background in OnPaint
:
Graphics g = e.Graphics;
g.Clear(this.BackColor);
回答4:
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.
来源:https://stackoverflow.com/questions/1300329/derived-datagridview-drawing-problem-shows-black-regions