Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF DataGrid
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 20-Nov-12 1:29am
bw2012128

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

  Print Answers RSS
0 OriginalGriff 325
1 Sergey Alexandrovich Kryukov 289
2 CPallini 275
3 DamithSL 260
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
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