更改列标题文本DataGridView
问题描述:
如标题所示。我的代码不工作,我不知道为什么。我查看了其他来源,他们似乎通过代码建议我正在尝试做什么。有人有主意吗?更改列标题文本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
谢谢!
答
如果您正在使用数据绑定到一个类型,自动生成列,你可以做这样的事情:
[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);
我没有。我需要添加一个TableStyle到我的网格视图吗? –
看到这个答案http://stackoverflow.com/questions/6829570/set-columns-width-in-a-datagrid-using-compact-framework :) –
检查我的更新 –