[C#]DataGridViewで、行ヘッダにある三角マーク”▲”を非表示にする


DataGridView使用時に行ヘッダを表示させると、選択行に三角のマークが表示されます。
表示専用のグリッドなどで、上記マークを非表示にしたい場合は以下の作業を行います。



DataGridViewが標準で使用しているRowHeaderCellクラスが表示させているので、まずはこれを継承したクラスを作成します。
最後の引数で指定されたpaintPartsを弄って、状態表示のアイコンを非表示にしています。

//---------------------------------------------
// 三角マーク非表示版の、DataGridView行ヘッダー
//---------------------------------------------
class DgvRowHeaderNoTriangle : DataGridViewRowHeaderCell {
    protected override void Paint(  Graphics                        graphics,
                                    Rectangle                       clipBounds,
                                    Rectangle                       cellBounds,
                                    int                             rowIndex,
                                    DataGridViewElementStates       cellState,
                                    Object                          value,
                                    Object                          formattedValue,
                                    string                          errorText,
                                    DataGridViewCellStyle           cellStyle,
                                    DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                    DataGridViewPaintParts          paintParts ) {
        paintParts &= ~DataGridViewPaintParts.ContentBackground;
        base.Paint( graphics, clipBounds, cellBounds, rowIndex, cellState, value, 
                    formattedValue, errorText, cellStyle, advancedBorderStyle,
                    paintParts );       
    }
};





上記クラスを定義後、画面のロード処理でグリッドのHeaderCellクラスを変更します。

private void Form1_Load( object sender, EventArgs e ) {
    DataGridView1.RowTemplate.HeaderCell = new DgvRowHeaderNoTriangle();
}




プログラム内で動的に、表示を元に戻したい場合は上記のプロパティをnullにすればよいです。

DataGridView1.RowTemplate.HeaderCell = null;

関連記事

コメントを残す

メールアドレスが公開されることはありません。