.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

WPF Cell Alignment In DataGrid

Posted By:Diptimaya Patra       Posted Date: May 07, 2010    Points: 25    Category: WPF    URL: http://www.dotnetspark.com  

WPF Cell Alignment In DataGrid. In this article we will see how we can change the Cell Alignment in DataGrid in WPF.


In this article we will see how we can change the Cell Alignment in DataGrid in WPF.

Crating WPF Application Project

Fire up Visual Studio 2008 and Create a WPF Application and name the project as CellAlignmentDataGridWPF.

Let's have a DataGrid and then we would bind some sample data to it, so that we can visualize the Data in DataGrid.

public class Employee
    public string Name { get; set; }
    public int Age { get; set; }
    public string EmailID { get; set; }
    public string Country { get; set; }
    public string Contact { get; set; }

The above class represents the structure of Employee Entity.

Now we would have some sample data and bind it to DataGrid.

public partial class Window1 : Window
    public Window1()

        ObservableCollection empData = new ObservableCollection 
                new Employee{Name="Diptimaya Patra", Contact="0000",Age=25,
                    EmailID="diptimaya.patra@some.com", Country="India"},
                new Employee{Name="Dhananjay Kumar", Contact="00020",Age=24, 
                    EmailID="dhananjay.kumar@some.com", Country="India"},
                new Employee{Name="David Paul", Contact="1230", Age=26,
                    EmailID="david.paul@some.com", Country="India"},
                new Employee{Name="Christina Joy", Contact="1980", Age=22,
                    EmailID="christina.joy@some.com", Country="UK"},
                new Employee{Name="Hiro Nakamura", Contact="0000", Age=26,
                    EmailID="hiro.nakamura@some.com", Country="Japan"},
                new Employee{Name="Angela Patrelli", Contact="0000", Age=29,
                    EmailID="angela.patrelli@some.com", Country="Japan"},
                new Employee{Name="Zoran White", Contact="0000", Age=43,
                    EmailID="diptimaya.patra@some.com", Country="Scotland"},
        dgData.ItemsSource = empData;

Now we will run the application and see how the cell alignement are look by default.

As you see in above display the default alignement of the DataGrid Cell is Left Alignment.

Now we would try to have Right and Center Alignement for Cells too.

We have to create a style that would Change the Template of DataGridCell, so let's do it.

The above styles are for DataGridCell. Now we will use Custom columns in our DataGrid and have these styles applied on.


Now let's see how does it look in run time.

As simple as that. So we have achieved the Cell Alignment in DataGrid.

You can also download the sample project used in above example

Hope this article helps.

 Subscribe to Articles


Further Readings:

Author: williamalbert         Company URL: http://www.dotnetspark.com
Posted Date: June 09, 2012

When a header is added to Grid.Headers collection, a zero-based index is assigned to it in the hierarchy. When you call Header.Add method, additional columns are added to a header. After that columns can be accessed with indexer operator.

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend