Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hallo,
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}" 
           ScrollViewer.CanContentScroll="True"
           ScrollViewer.VerticalScrollBarVisibility="Auto" 
           ScrollViewer.HorizontalScrollBarVisibility="Auto">

	   <DataGrid.Resources>
             <Style  TargetType="DataGridColumnHeader">
               <Setter Property="Template">
                  <Setter.Value>
                    <ControlTemplate TargetType="DataGridColumnHeader">
                       <ItemsControl ItemsSource="{Binding Labels}" >
                         <ItemsControl.ItemTemplate>
                           <DataTemplate>
                             <Label Content="{Binding Text}" 
                              Grid.Column="{Binding ColumnIndex}"
                              Grid.Row="{Binding RowIndex}" />
                           </DataTemplate>
                         </ItemsControl.ItemTemplate>
                       </ItemsControl>
                    </ControlTemplate>
                  </Setter.Value>
               </Setter>
              </Style>
           </DataGrid.Resources>
 	</DataGrid>




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));
    i++;
 }           

In my ViewModel I have:

 public ObservableCollection<DataGridColumnHeader> Labels
 {
   get { return labels; }
   set { labels = value; }
 }

public DataView DV1
{
  get 
    {
      mgr = new DataTableManager("Table1", FilteredParameters[0]);
      mgr.AddRows(FilteredData);
      return mgr.GetView();
    }
}

I don't understand why I have the following result:

all headers are in one column.

header1
header2
header3
......

Sorry for my bad English and thanks for help.
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900