방법: Windows Forms DataGrid 컨트롤에서 열 삭제 또는 숨기기

참고

DataGridView 컨트롤은 DataGrid 컨트롤을 대체하고 여기에 다른 기능을 추가하여 새로 도입된 컨트롤이지만 이전 버전과의 호환성 및 이후 사용 가능성을 고려하여 DataGrid 컨트롤을 계속 유지하도록 선택할 수 있습니다. 자세한 내용은 Windows Forms DataGridView 컨트롤과 DataGrid 컨트롤의 차이점을 참조하세요.

GridColumnStylesCollectionDataGridColumnStyle 개체(DataGridTableStyle 클래스의 멤버)의 속성과 메서드를 사용하여 Windows Forms DataGrid 컨트롤에서 열을 프로그래밍 방식으로 삭제하거나 숨길 수 있습니다.

삭제되거나 숨겨진 열은 그리드가 바인딩된 데이터 원본에 여전히 존재하며 프로그래밍 방식으로 계속 액세스할 수 있습니다. DataGrid에서 더 이상 표시되지 않을 뿐입니다.

참고

애플리케이션이 특정 데이터 열에 액세스하지 않고 DataGrid에 해당 열을 표시하지 않으려면 애초에 데이터 원본에 포함할 필요가 없습니다.

DataGrid에서 프로그래밍 방식으로 열을 삭제하려면

  1. 양식의 선언 영역에서 DataGridTableStyle 클래스의 새 인스턴스를 선언합니다.

  2. 스타일을 적용하려는 데이터 원본의 테이블로 DataGridTableStyle.MappingName 속성을 설정합니다. 다음 예제에서는 이미 설정된 것으로 가정하는 DataGrid.DataMember 속성을 사용합니다.

  3. DataGrid의 테이블 스타일 컬렉션에 새 DataGridTableStyle 개체를 추가합니다.

  4. DataGridGridColumnStyles컬렉션의 RemoveAt 메서드를 호출하고 삭제할 컬렉션의 열 인덱스를 지정합니다.

    ' Declare a new DataGridTableStyle in the  
    ' declarations area of your form.  
    Dim ts As DataGridTableStyle = New DataGridTableStyle()  
    
    Sub DeleteColumn()  
       ' Set the DataGridTableStyle.MappingName property  
       ' to the table in the data source to map to.  
       ts.MappingName = DataGrid1.DataMember  
    
       ' Add it to the datagrid's TableStyles collection  
       DataGrid1.TableStyles.Add(ts)  
    
       ' Delete the first column (index 0)  
       DataGrid1.TableStyles(0).GridColumnStyles.RemoveAt(0)  
    End Sub  
    
    // Declare a new DataGridTableStyle in the  
    // declarations area of your form.  
    DataGridTableStyle ts = new DataGridTableStyle();  
    
    private void deleteColumn()  
    {  
       // Set the DataGridTableStyle.MappingName property  
       // to the table in the data source to map to.  
       ts.MappingName = dataGrid1.DataMember;  
    
       // Add it to the datagrid's TableStyles collection  
       dataGrid1.TableStyles.Add(ts);  
    
       // Delete the first column (index 0)  
       dataGrid1.TableStyles[0].GridColumnStyles.RemoveAt(0);  
    }  
    

DataGrid에서 프로그래밍 방식으로 열을 숨기려면

  1. 양식의 선언 영역에서 DataGridTableStyle 클래스의 새 인스턴스를 선언합니다.

  2. 스타일을 적용하려는 데이터 원본의 테이블로 DataGridTableStyleMappingName 속성을 설정합니다. 다음 코드 예제에서는 이미 설정된 것으로 가정하는 DataGrid.DataMember 속성을 사용합니다.

  3. DataGrid의 테이블 스타일 컬렉션에 새 DataGridTableStyle 개체를 추가합니다.

  4. Width 속성을 0으로 설정하여 열을 숨기고 숨길 열의 열 인덱스 지정합니다.

    ' Declare a new DataGridTableStyle in the  
    ' declarations area of your form.  
    Dim ts As DataGridTableStyle = New DataGridTableStyle()  
    
    Sub HideColumn()  
       ' Set the DataGridTableStyle.MappingName property  
       ' to the table in the data source to map to.  
       ts.MappingName = DataGrid1.DataMember  
    
       ' Add it to the datagrid's TableStyles collection  
       DataGrid1.TableStyles.Add(ts)  
    
       ' Hide the first column (index 0)  
       DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 0  
    End Sub  
    
    // Declare a new DataGridTableStyle in the  
    // declarations area of your form.  
    DataGridTableStyle ts = new DataGridTableStyle();  
    
    private void hideColumn()  
    {  
       // Set the DataGridTableStyle.MappingName property  
       // to the table in the data source to map to.  
       ts.MappingName = dataGrid1.DataMember;  
    
       // Add it to the datagrid's TableStyles collection  
       dataGrid1.TableStyles.Add(ts);  
    
       // Hide the first column (index 0)  
       dataGrid1.TableStyles[0].GridColumnStyles[0].Width = 0;  
    }  
    

참고 항목