简体   繁体   中英

Alter Column Heading Text DataGridView

As title says. My code is not working, and I am not sure why. I've looked at other sources and they seem to suggest what I am trying to do below via code. Anyone have any ideas?

I also tried .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

Thanks!

If you are using data-binding to a type and auto-generated columns you can do something like this:

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

In another way, as you said, you should use:

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

But here something else I did not try. Also try something like this:

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

But as this answer says, you should do something like 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM