私は今日、同じ「問題」を抱えていました。また、特定の行のボタンを非表示にしたいと考えていました。しばらく遊んだ後、オーバーロードされた paint()
を必要としない、非常にシンプルで優れたソリューションを発見しました。 -関数または類似のもの:
別の DataGridViewCellStyle
を割り当てるだけです それらの細胞に。
キーは、 padding
を設定することです この新しいスタイルのプロパティを、セルの表示領域からボタン全体をシフトする値に変更します。
それでおしまい! :-)
サンプル:
System::Windows::Forms::DataGridViewCellStyle^ dataGridViewCellStyle2 = (gcnew System::Windows::Forms::DataGridViewCellStyle());
dataGridViewCellStyle2->Padding = System::Windows::Forms::Padding(25, 0, 0, 0);
dgv1->Rows[0]->Cells[0]->Style = dataGridViewCellStyle2;
// The width of column 0 is 22.
// Instead of fixed 25, you could use `columnwidth + 1` also.
Tobias の回答に基づいて、パディングを調整してセルの内容を非表示にする小さな静的ヘルパー メソッドを作成しました。
ただし、ユーザーがセルを選択してスペースを押すと非表示のボタンがクリックされるという点で、ボタンは引き続き「クリック可能」であることに注意してください。そのため、 contentclick イベントでクリックを処理する前に、セルの値が読み取り専用でないことを確認します
public static void DataGridViewCellVisibility(DataGridViewCell cell, bool visible)
{
cell.Style = visible ?
new DataGridViewCellStyle { Padding = new Padding(0, 0, 0, 0) } :
new DataGridViewCellStyle { Padding = new Padding(cell.OwningColumn.Width, 0, 0, 0) };
cell.ReadOnly = !visible;
}
パディングは私にはうまくいきませんでした。セルを空のテキストセルにする方が簡単できれいだと思います。 VB ですが、アイデアはわかります:
Dim oEmptyTextCell As New DataGridViewTextBoxCell()
oEmptyTextCell.Value = String.Empty
oRow.Cells(i) = oEmptyTextCell