简体   繁体   中英

WPF - Changing Column Name on Data Bound DataGrid

Basically I'm using the ItemSource property of the datagrid to bind a generic list to my datagrid. However I'd really like to change the headings, I tried the following but I get a runtime exception:

dgtest.Columns[1].Header = "edited";

I used the AutoGeneratingColumn event and an Attribute to set my column names.

First create an attribute class...

    public class ColumnNameAttribute : System.Attribute
    {
        public ColumnNameAttribute(string Name) { this.Name = Name; }
        public string Name { get; set; }
    }

Then I decorate my data class members with the new attribute...

    public class Test
    {
        [ColumnName("User Name")]
        public string Name { get; set; }
        [ColumnName("User Id")]
        public string UserID { get; set; }
    }

Then I write my AutoGeneratingColumn event handler...

    void dgPrimaryGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        var desc = e.PropertyDescriptor as PropertyDescriptor;
        var att = desc.Attributes[typeof(ColumnNameAttribute)] as ColumnNameAttribute;
        if(att != null)
        {
            e.Column.Header = att.Name;
        }
    }

... and attach it to my grid and test...

        dgPrimaryGrid.AutoGeneratingColumn += dgPrimaryGrid_AutoGeneratingColumn;

        var data = new object[] 
        {
            new Test() { Name = "Joe", UserID = "1" }
        };
        dgPrimaryGrid.ItemsSource = data;

Here is what it looks like. Notice that the column names are not the property names (the default behavior).

列重命名的 DataGrid

This approach is a little more work, but it's nice to have the column heading defined at the same place as the bound column. You can reorder your columns without having to go to other places to fix c the column names.

You can change it on the ItemDataBound event:

public void yourDataGrid_OnItemDataBound(object s, DataGridItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
        // Change the cell index to the column index you want... I just used 0
        e.Item.Cells[0].Text = "Text you want in header.";
    }
}

If the grid is already bound you should be able to do:

yourDataGrid.Columns[0].Header = "Text you want in header.";

You are probably getting an error because you are trying to change the text before it is bound.

AutoGeneratedColumns event on wpf for change column name

datagrid1.AutoGeneratedColumns += datagrid1_AutoGeneratedColumns;

void datagrid1_AutoGeneratedColumns(object sender, EventArgs e)
{
    datagrid1.Columns[0].Header = "New Column Name";
}

1) Switch off the automatic column generation and generate your data grid columns in the program code:

DataGridTextColumn TempColumn;    

MyDataGrid.AutoGenerateColumns = false;

TempColumn = new DataGridTextColumn();
TempColumn.Header = "DisplayName0";
TempColumn.Binding = new Binding("BindingName0");
MyDataGrid.Columns.Add(TempColumn);

TempColumn = new DataGridTextColumn();
TempColumn.Header = "DisplayName1";
TempColumn.Binding = new Binding("BindingName1");
MyDataGrid.Columns.Add(TempColumn);

Then "BindigName0" is the internal binding name of column 0 and "DisplayName0" is the name that the user will see.

2) If you want to use the automatic column generation instead then the display names of the columns can be set in the "AutoGeneratingColumn" event:

MyDataGrid.AutoGeneratingColumn += MyDataGrid_AutoGeneratingColumn;

...

private void MyDataGrid_AutoGeneratingColumn(object sender, 
              DataGridAutoGeneratingColumnEventArgs e)
{
  DataGridBoundColumn TempColumn;
  string BindingName;

  if (e.Column is DataGridBoundColumn)
  {
    TempColumn = e.Column as DataGridBoundColumn;
    BindingName = (TempColumn.Binding as Binding).Path.Path;
    if (BindingName == "BindingName0")
    {
      TempColumn.Header = "DisplayName0";
    }
    else if (BindingName == "BindingName1")
    {
      TempColumn.Header = "DisplayName1";
    }
  }
}

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