Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0 DataGrid
 
  private void dataGrid1_AutoGeneratedColumns(object sender, EventArgs e)
        {
          
 
            Style mystyle = new System.Windows.Style();           
            Setter set = new Setter();
            set.Property = DependencyProperty.Register("TextAlignment", typeof(TextAlignment),
                typeof(Setter));
            mystyle.TargetType = typeof(TextBlock);
            set.Value = TextAlignment.Right;
            mystyle.Setters.Add(set);
           
            dataGrid1.AutoGenerateColumns = true;
            
 
             DataGridColumnHeader header =new DataGridColumnHeader();
             Style headerstyle = new System.Windows.Style();
            headerstyle.TargetType = header.GetType();            
            Setter setter = new Setter();
            Setter setter1 = new Setter();
            Setter setter2 = new Setter();
            setter.Property = FontSizeProperty;
            FontSize = 15;
            setter.Value = FontSize;
            headerstyle.Setters.Add(setter);
            setter1.Property = ForegroundProperty;
            Foreground = Brushes.BlueViolet;
            setter1.Value = Foreground;
            headerstyle.Setters.Add(setter1);      
            Setter setter3 = new Setter();      
            LinearGradientBrush lb = new LinearGradientBrush();
            Point startpoint=new Point(0.5,0);
            Point endpoint = new Point(1, 1);
            lb.StartPoint = startpoint;
            lb.EndPoint = endpoint;
            GradientStop gst = new GradientStop();
            lb.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));
            lb.GradientStops.Add(new GradientStop(Colors.Orange, 0.5));
            lb.GradientStops.Add(new GradientStop(Colors.Red, 1.0));           
            setter3.Property = BackgroundProperty;
            setter3.Value = lb;
            headerstyle.Setters.Add(setter3);
            headerstyle.Setters.Add(set);
          dataGrid1.HeaderStyle = headerstyle;
 

 
this code yield datagrid1 Headerstyle change . but the text alignment in the column can't be set it always lie on the left
what I have to do for this
 
any help is highly appreaciated
Posted 23-Jan-12 4:34am
(unknown member)
Edited 24-Jan-12 4:06am
v2

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

  Print Answers RSS
0 OriginalGriff 200
1 PIEBALDconsult 150
2 BillWoodruff 148
3 Jochen Arndt 135
4 DamithSL 130
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 24 Jan 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