Click here to Skip to main content
11,920,772 members (57,696 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# WPF DataGrid
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 1:29am

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

  Print Answers RSS
0 OriginalGriff 548
1 Dave Kreskowiak 245
2 phil.o 170
3 KrunalRohit 134
4 F-ES Sitecore 130
0 OriginalGriff 6,778
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,203
3 George Jonsson 2,815
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web03 | 2.8.151120.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2015
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