Click here to Skip to main content
13,086,662 members (55,664 online)
Rate this:
Please Sign up or sign in to vote.
See more:
my task is to write DataGrid that has special characters in its headers and is rich on functionality. I make headers dynamically (the table can have variable number of columns). I have set Binding to DataTable by DataView: ItemsSource="{Binding DV1}". The problem with DataTable is that it doesn't support headers with special characters like ".","/","§" ... and I have a lot of them. (Not only of this reason the DataTable SHOULD be used in my case). So I try to overwrite the headers of DataTable by <DataGrid.Resources>, so that they should be clickable to sort the table data as well as it is possible by DataTable. 
I have a DataGrid
 	<DataGrid AutoGenerateColumns="True" 
           ColumnHeaderStyle="{StaticResource HeaderStyle}"
           CanUserAddRows="False" ItemsSource="{Binding DV1}" 
             <Style  TargetType="DataGridColumnHeader">
               <Setter Property="Template">
                    <ControlTemplate TargetType="DataGridColumnHeader">
                       <ItemsControl ItemsSource="{Binding Labels}" >
                             <Label Content="{Binding Text}" 
                              Grid.Column="{Binding ColumnIndex}"
                              Grid.Row="{Binding RowIndex}" />


public class DataGridColumnHeader 
        public DataGridColumnHeader(string labelName, int row, int column)
            Text = labelName;
            RowIndex = row;
            ColumnIndex = column;

        private string text;
        public string Text
          get { return text; }
          set { text = value;}
        private int rowIndex;
        public int RowIndex
          get { return rowIndex; }
          set { rowIndex = value; }
        private int columnIndex;
        public int ColumnIndex
          get { return columnIndex; }
          set { columnIndex = value; }
In constructor of my ViewModel I create from Parameters of type List<KeyValuePair<int, string>> my DataTable headers:
private void InitLabelsCollections()
 labels = new ObservableCollection<DataGridColumnHeader>();
 int i = 0;
 foreach (KeyValuePair<int, string> e in Parameters)
   labels.Add(new DataGridColumnHeader(e.Value, 0, i));
In my ViewModel I have:
 public ObservableCollection<DataGridColumnHeader> Labels
   get { return labels; }
   set { labels = value; }
public DataView DV1
      mgr = new DataTableManager("Table1", FilteredParameters[0]);
      return mgr.GetView();
I don't understand why I have the following result:
all headers are in one column.
Sorry for my bad English and thanks for help.
Posted 20-Nov-12 0:29am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100