2014-07-21 66 views
0

當我爲Windows CE開發時,我有一個名爲DataGrid的組件。現在,在最新版本中使用Windows窗體,我只有DataGridView,它沒有TableStyles。我試圖操縱它的DataGridTextBoxColumn,但我無法做到這一點。我綁定了KeyValuePair列表中的數據,我不知道如何控制其列的某些內容,如MappingName,HeaderText或Width。 我的代碼目前是以下:添加TableStyles到DataGridView

public IList<KeyValuePair<int, string>> Lista 
{ 
    set 
    { 
     dgwWorkitem.DataSource = value; 
     DataGridTableStyle tabela = new DataGridTableStyle 
     { 
      MappingName = dgwWorkitem.GetType().Name 
     }; 
     DataGridTextBoxColumn id = new DataGridTextBoxColumn 
     { 
      MappingName = @"Key", 
      HeaderText = @"Id", 
      Width = (((dgwWorkitem.Width)/2)/2) 
     }; 
     DataGridTextBoxColumn titulo = new DataGridTextBoxColumn 
     { 
      MappingName = @"Value", 
      HeaderText = @"Título", 
      Width = (((dgwWorkitem.Width)/2) + ((dgwWorkitem.Width/2)/2)) 
     }; 
     tabela.GridColumnStyles.Add(id); 
     tabela.GridColumnStyles.Add(titulo); 
     // dgwWorkitem.TableStyles.Clear(); 
     // dgwWorkitem.TableStyles.Add(tabela); 
    } 
} 

我用set將數據綁定後,我想操縱,但是從DataGridTableStyles沒有在DataGridView接受。如何操作它們而不創建DataGridTableStyle

回答

0

你只是不需要創建一個新的DataGridTableStyle。知道它的指標,只是操作的每個屬性分別:

public IList<KeyValuePair<int, string>> Lista 
{ 
    set 
    { 
     dgwWorkitem.DataSource = value; 
     dgwWorkitem.Columns[0].HeaderText = @"Id"; 
     dgwWorkitem.Columns[0].Width = (((dgwWorkitem.Width)/2)/2); 
     dgwWorkitem.Columns[1].HeaderText = @"Título"; 
     dgwWorkitem.Columns[1].Width = (((dgwWorkitem.Width)/2) + ((dgwWorkitem.Width/2)/2)); 
    } 
} 

這種方式可以處理IdTítulo像你想這樣做,和以前一樣。