C#、Winforms、および.Net3.5を使用しています
私のフォームにはカスタムがありますDataGridView
(ここに示すように、セルフォーマットイベント中のちらつきを防ぐためにダブルバッファリングされています)。データベース検索を実行するとき、結果のデータセットをにバインドしますdatagridview
。
CellFormatting
データに応じて、行を特定の色でペイントするイベントを処理します。
私のDataGridViewコード:
resultsGridView.DataSource = results.DefaultViewManager.DataSet.Tables[0];
resultsGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue;
resultsGridView.BorderStyle = BorderStyle.Fixed3D;
resultsGridView.CellFormatting += new DataGridViewCellFormattingEventHandler(resultsGridView_CellFormatting);
私のCellFormattingコード:
void resultsGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int rowIndex = e.RowIndex;
DataGridViewRow therow = resultsGridView.Rows[rowIndex];
if ((bool)therow.Cells["Sealed"].Value == true)
{
therow.DefaultCellStyle.BackColor = Color.Pink;
}
if (therow.Cells["Database"].Value as string == "PNG")
{
therow.DefaultCellStyle.BackColor = Color.LightGreen;
}
}
CellFormattingを処理すると、フォーム全体のPaintイベントがオフになっているように見えることを除いて、すべてがうまく機能します。テキストボックスでカーソルの点滅が止まり、フォームのメニューストリップは次のようになります。
上は検索前、下は検索後です。メニュー項目がある場所にマウスを合わせるまでメニューバーは再描画されません。その後、マウスをメニューバーの外に移動しても、最後に強調表示される項目はそのままになります。フォームを移動すると再描画されるようですが、問題は残ります。
resultsGridView.CellFormatting
datagridviewコードの行をコメントアウトすると、問題が完全に修正されます。
セルを間違ってペイントしていますか、それとも他に処理する必要があるものがありますか?