2017-11-17 6 views
0

winforms DataGridViewで列を区切る行の一部だけを非表示にすることはできますか?DataGridViewのすべての列ではなく、一部の列間の線を非表示にするにはどうすればよいですか?

myDataGridView.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;は、グリッド全体に垂直線を非表示にしますが、特定の垂直線を非表示にするにはどうすればよいですか?

+0

自分でCellPaintメソッドに入り、必要な行をペイントする必要があります。 – LarsTech

+0

データベーステーブルでdatagridviewを塗りつぶした場合、それらの値をselect文に追加する方が簡単でしょうか? –

+0

@antonio_venerosoこれはデータベースからのものですが、手動で入力されたデータバインドされた2列のDataGridViewImageColumnの間にカスタムの列があり、アイコンとテキストの間の線を削除したいとします。 – Jonah

答えて

0

DataGridViewコントロールは、単一セルに対してカスタマイズできます。
しかし、そのカスタマイズはややこしいかもしれません。

カスタムセル/列形式またはビヘイビアが必要な場合は、内部メカニズムをオーバーライドする必要があります(複雑ではありません)。

まず、カスタムの列/セルクラスを作成し、それらの基本クラスから派生させて、新しいデフォルトプロパティを設定します。

あなたはDataGridViewの列にビットマップを表示したいので、1を構築することができます:

public class MyCustomColumn : DataGridViewColumn 
{ 
    public MyCustomColumn() 
    { 
     this.CellTemplate = new MyCustomCell(); 
     //Set the .ValueType of its Cells to Bitmap 
     this.CellTemplate.ValueType = typeof(System.Drawing.Bitmap); 
    } 
} 

次に、あなたがあなたのコラムセルの新しいプロパティを定義します。 いくつかのデフォルトのプロパティは、いつ 内部エラーを防止するために必要な新しいセルが作成されます。デフォルト画像を初期化する必要があります。

public class MyCustomCell : DataGridViewImageCell 
{ 
    public MyCustomCell() 
     : this(true) { } 

    public MyCustomCell(bool valueIsIcon) 
    { 
     //Set the .ImageLayout to "Zoom", so the image will scale 
     this.ImageLayout = DataGridViewImageCellLayout.Zoom; 
     //This is the pre-defined image if no image is set as Value. You can set this to whaterver you want. 
     //Here I'm defining an invisible bitmap of 1x1 pixels 
     this.Value = new System.Drawing.Bitmap(1,1); 
     //This is an internal switch. Doesn't mean much here. Set to true to comply with the standard. 
     this.ValueIsIcon = valueIsIcon; 
    } 

    public override DataGridViewAdvancedBorderStyle AdjustCellBorderStyle(
     DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStyleInput, 
     DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStylePlaceHolder, 
     bool singleVerticalBorderAdded, 
     bool singleHorizontalBorderAdded, 
     bool firstVisibleColumn, 
     bool firstVisibleRow) 
    { 
     //Set the new values for these cells borders, leaving out the right one. 
     dataGridViewAdvancedBorderStylePlaceHolder.Right = DataGridViewAdvancedCellBorderStyle.None; 

     dataGridViewAdvancedBorderStyleInput.Top = DataGridViewAdvancedCellBorderStyle.Single; 
     dataGridViewAdvancedBorderStylePlaceHolder.Bottom = DataGridViewAdvancedCellBorderStyle.Single; 
     dataGridViewAdvancedBorderStyleInput.Left = DataGridViewAdvancedCellBorderStyle.Single; 

     return dataGridViewAdvancedBorderStylePlaceHolder; 
    } 
} 

最後に、新しい列をインスタンス化し、DataGridViewのColumnsコレクションに追加します。 カスタム列をコレクション内の任意の有効な位置に挿入できます。

あなたのForm_Loadイベントにこのコードを配置することができます:もちろん

MyCustomColumn _MyColumn = new MyCustomColumn(); 
this.dataGridView1.Columns.Insert(2, _MyColumn); 

あなたは、さらにあなたの細胞のフォーマットをカスタマイズするための他のプロパティを設定することができます。 。

関連する問題