2013-05-07 3 views
1

C# Winform에서 DataGridView의 내용을 새로 고치고 시각적 모양을 유지하려고합니다.DataGridView 내용 새로 고침

그것은, 데이터 소스를 업데이트 모든 열을 지우고 적절한 헤더 텍스트에 필요한 자들을 돌아 오게 : 나는 새로 고침을 할 수

유일한 방법은 다시 초기화 그리드를이 기능입니다.

private void InitDataGrid() 
{ 
    allItem = DataRepository.LotProvider.GetByIdProduit(detail.IdProduit) 
    dataGridView1.DataSource = allItem; 
    dataGridView1.Columns.Clear(); 
    // Get a dictionary of the required column ID/shown text 
    Dictionary<string, string> dictionary = InitDisplayedFields();   
    foreach (KeyValuePair<string, string> column in dictionary) 
     // If the grid does not contain the key 
     if (!dataGridView1.Columns.Contains(column.Key)) 
     { 
      // Add the column (key-value) 
      int id = dataGridView1.Columns.Add(column.Key, column.Value); 
      // Bind the property 
      dataGridView1.Columns[id].DataPropertyName = column.Key; 
     } 
} 

문제는 사용자가 X 크기 Y 크기에서 열을 뻗어하고있는 DataGridView를 새로 경우, 열이 다시 Y 크기로 갈 것입니다.

그렇지 않으면 그리드가 초기화 될 때 모든 데이터 소스 열을 표시하기 때문에 Clear()를 사용할 수 없었습니다.

내가 원하는 것은 모든 확장 된 열의 X 크기를 유지하는 것입니다.

현재 코드를 최적화하기 위해 어떤 제안이라도 고맙게 생각합니다.

답변

0

새로운 데이터 원본을 바인딩하기 전에 열의 크기를 캡처해야한다고 생각합니다. 전에이 같은 뭔가

당신은

List<int> widthsList= new List<int>(); 
foreach (var column in dataGridView1.Columns) 
{ 
    int ColumnWidth = column.Width; 
    widthsList.Add(ColumnWidth); 
} 

바인딩이 당신이

int j=0; 
foreach (int wz in widthsList) 
{ 
    dataGridView1.Columns[j].Width = wz ; 
    j++; 
} 
+0

을 결합 후를 DataColumn 클래스는 더 폭 속성이하거나 임의 UI 관련 속성이 포함되어 보인다. 대신 dataGridView1.Columns [index]를 통해 직접 액세스해야합니다. –

+0

업데이트를 시도하십시오. –

+0

var을 Width 속성에 대한 액세스 권한이있는 DataGridViewColumn으로 바꿉니다. 훌륭하게 작동합니다. –