2017-11-17 1 views
0

winforms DataGridView에서 열을 구분하는 일부 줄만 숨길 수 있습니까?DataGridView의 일부 열 (모든 열이 아닌) 사이에 선을 숨기려면 어떻게해야합니까?

myDataGridView.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal;은 전체 눈금에 수직선을 숨기지 만 특정 수직선을 어떻게 숨길 수 있습니까?

+0

직접 CellPaint 메서드에 들어가서 원하는 선을 페인트해야합니다. – LarsTech

+0

datagridview에 데이터베이스 테이블을 채우는 경우 select 문에서 해당 값을 조인하는 것이 더 쉽지 않습니까? –

+0

@antonio_veneroso 데이터베이스에서 왔지만 수동으로 채워진 DataGridViewImageColumn 유형의 두 데이터 바인딩 된 열 사이에 맞춤 항목이 있으며 아이콘과 텍스트 사이의 선을 제거하려고합니다. – Jonah

답변

0

DataGridView 컨트롤은 단일 셀에 맞게 사용자 지정할 수 있습니다.
사용자 정의가 다소 까다로울 수 있습니다.

셀/열 형식 또는 동작을 사용자 지정하려면 내부 메커니즘을 재정의해야합니다 (실제로 그렇게 복잡하지는 않음).

먼저 기본 클래스에서 파생 된 사용자 지정 열/셀 클래스를 만든 다음 새 기본 속성을 설정합니다.

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); 
    } 
} 

그런 다음 당신이 당신의 열 세포의 새로운 속성 정의 :

당신이 DataGridView에 열에서 비트 맵을 표시 할 보낸 사람, 하나를 구축 할 수 있습니다 일부 기본 속성은 내부 오류를 방지하는 데 필요한을 때 새로운 셀이 생성됩니다. 기본 이미지를 초기화해야합니다. 결국

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의 열 컬렉션에 추가합니다. 사용자 지정 열을 컬렉션의 유효한 위치에 삽입 할 수 있습니다.

당신은 당신의 Form_Load 이벤트에서이 코드를 배치 할 수 있습니다 :

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

은 물론 당신이 더 당신의 세포 형식을 사용자 정의 할 다른 속성을 설정할 수 있습니다. .

관련 문제