2012-03-16 2 views
0

DataGrid가 포함 된 .NET Compact Framework 3.5 응용 프로그램에서 작업하고 있습니다. 디자이너를 사용하여 BindingSource를 만들고 바인딩 소스를 DataGrid의 소스로 추가했습니다. 그것은 내 원본 개체 유형의 모든 적합한 속성에 대한 열을 자동으로 만들었지 만 모든 속성을 표시하고 싶지는 않습니다.DataGrid에 표시된 열을 선택하는 방법

표시 할 열과 숨길 열을 어떻게 지정합니까? 내가 DataGrid의 TableStyles 속성 (코드와 디자이너 모두)을 가지고 놀아 보았는데 아무런 효과가없는 것처럼 보였다.

+0

DataGridView 컨트롤 용입니까? – jp2code

+0

아니요, .NET CF 3.5 DataGrid. –

답변

1

알아 냈어. 나는 바로 InitializeComponent() 후, 폼의 생성자에서 다음 코드 줄을 추가했다 :

myDataGrid.TableStyles[0].MappingName = myBindingSource.GetListName(null); 

그럼 내가 원하는대로 표시되는 열을 수정하기 위해 데이터 그리드의 TableStyles 속성을 변경 할 수 있었다.

3

DataGrid 컨트롤의 TableStyles 컬렉션이 지정한 시점에 비어 있으며 DataGridTableStyle 컬렉션이 추가 될 때까지 남아 있습니다.

MappingName 속성에 대한 올바른 값을 설정하기위한 귀하의 제안을 사용하여, 내가 만들고 DataGrid에서 요구 된 유일한 공공 필드를 포함하는 새로운 DataGridTableStyle 객체를 추가하여 원하는 결과를 달성했다.

// Create a DataGridTableStyle to hold all the columns to be displayed in the DataGrid 
DataGridTableStyle myTableStyle = new DataGridTableStyle(); 
myTableStyle.MappingName = myBindingSource.GetListName(null); // This is the magic line 
myTableStyle.GridColumnStyles.Clear(); 

// Add some DataGridColumnStyles 
DataGridTextBoxColumn columnRowId = new DataGridTextBoxColumn(); 
columnRowId.MappingName = "idx"; //This must match the name of the public property 
ColumnRowId.HeaderText = "Record"; 
tableStyleReportsSummary.GridColumnStyles.Add(columnRowId); 

// Add the table style to the DataGrid 
myDataGrid.TableStyles.Clear(); 
myDataGrid.TableStyles.Add(myTableStyle); 
관련 문제