방법: Windows Forms DataGrid 컨트롤에서 열 삭제 또는 숨기기
업데이트: 2007년 11월
참고: |
---|
DataGridView는 DataGrid 컨트롤에 새로운 기능이 추가된 것으로, 이전 컨트롤을 대체합니다. 그러나 이전 버전과의 호환성 및 앞으로의 사용 가능성을 고려하여 DataGrid 컨트롤을 유지하도록 선택할 수 있습니다. 자세한 내용은 Windows Forms DataGridView 컨트롤과 DataGrid 컨트롤의 차이점을 참조하십시오. |
DataGridTableStyle 클래스의 멤버인 GridColumnStylesCollection 및 DataGridColumnStyle 개체의 속성과 메서드를 사용하면 Windows Forms DataGrid 컨트롤의 열을 프로그래밍 방식으로 삭제하거나 숨길 수 있습니다.
삭제되거나 숨겨진 열은 해당 데이터 표가 바인딩된 데이터 소스에 그대로 남아 있으므로 프로그래밍 방식으로 계속 액세스할 수 있습니다. 이러한 열은 데이터 표에 더 이상 나타나지 않을 뿐입니다.
참고: |
---|
응용 프로그램에서 액세스하지 않는 특정 데이터 열을 데이터 표에 표시하지 않으려면 데이터 소스에 해당 데이터 열을 포함할 필요가 없습니다. |
프로그래밍 방식으로 DataGrid에서 열을 삭제하려면
폼의 선언 영역에서 DataGridTableStyle 클래스의 새 인스턴스를 선언합니다.
DataGridTableStyle.MappingName 속성을 데이터 소스에서 스타일을 적용할 테이블로 설정합니다. 다음 예제에서는 DataGrid.DataMember 속성을 이미 설정한 것으로 가정하여 사용합니다.
새 DataGridTableStyle 개체를 데이터 표의 테이블 스타일 컬렉션에 추가합니다.
DataGrid의 GridColumnStyles 컬렉션에 대한 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에서 열을 숨기려면
폼의 선언 영역에서 DataGridTableStyle 클래스의 새 인스턴스를 선언합니다.
DataGridTableStyle의 MappingName 속성을 데이터 소스에서 스타일을 적용할 테이블로 설정합니다. 다음 코드 예제에서는 DataGrid.DataMember 속성을 이미 설정한 것으로 가정하여 사용합니다.
새 DataGridTableStyle 개체를 데이터 표의 테이블 스타일 컬렉션에 추가합니다.
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; }