Click here to Skip to main content
11,640,445 members (65,694 online)
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 3:34am
Edited 24-Jan-12 3: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 Mika Wendelius 300
1 CPallini 209
2 OriginalGriff 165
3 Sergey Alexandrovich Kryukov 139
4 Afzaal Ahmad Zeeshan 130
0 Mika Wendelius 200
1 OriginalGriff 145
2 CPallini 115
3 jgakenhe 40
4 sasanka sekhar panda 30


Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 24 Jan 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