2014-01-13 42 views
1

如標題所示。我的代碼不工作,我不知道爲什麼。我查看了其他來源,他們似乎通過代碼建議我正在嘗試做什麼。有人有主意嗎?更改列標題文本DataGridView

我也試過.Columns(0).Name = New Heading Name

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 

    With DataGridView1 
     .DefaultCellStyle.SelectionBackColor = Color.DodgerBlue 
     .DefaultCellStyle.SelectionForeColor = Color.White 
     .ColumnHeadersDefaultCellStyle.BackColor = Color.DodgerBlue 
     .ColumnHeadersDefaultCellStyle.BackColor = Color.AntiqueWhite 
     .DefaultCellStyle.Font = New Font("Calibri", 12, FontStyle.Regular) 

     .Columns(0).HeaderCell.Value = "Employee ID" 
     .Columns(1).HeaderCell.Value = "Account Number" 
     .Columns(2).HeaderCell.Value = "Last Name" 
     .Columns(3).HeaderCell.Value = "First Name" 
     .Columns(4).HeaderCell.Value = "Company" 
     .Columns(5).HeaderCell.Value = "Beginning Date" 
     .Columns(6).HeaderCell.Value = "Ending Date" 
    End With 

謝謝!

回答

1

如果您正在使用數據綁定到一個類型,自動生成列,你可以做這樣的事情:

[DisplayName("Header Name")] 
public string fieldName{get;set;} 

以另一種方式,就像你說的,你應該使用:

grid.Columns[0].HeaderText = "Header Name"; 

但在這裏別的我沒試過。也可以嘗試這樣的事:

myDataGrid.TableStyles[0].GridColumnStyles[0].HeaderText = "Header Name" 

但作爲this回答說,你應該做這樣的事情:

dataGrid1.TableStyles.Clear(); 
DataGridTableStyle tableStyle = new DataGridTableStyle(); 
tableStyle.MappingName = t.TableName; 
foreach (DataColumn item in t.Columns) 
{ 
    DataGridTextBoxColumn tbcName = new DataGridTextBoxColumn(); 
    tbcName.Width = 100; 
    tbcName.MappingName = item.ColumnName; 
    tbcName.HeaderText = item.ColumnName; 
    tableStyle.GridColumnStyles.Add(tbcName); 
} 
dataGrid1.TableStyles.Add(tableStyle); 
+0

我沒有。我需要添加一個TableStyle到我的網格視圖嗎? –

+0

看到這個答案http://stackoverflow.com/questions/6829570/set-columns-width-in-a-datagrid-using-compact-framework :) –

+0

檢查我的更新 –