SIGN UP MEMBER LOGIN:    
ARTICLE

Cell Alignment in DataGrid in WPF

Posted by Diptimaya Patra Articles | WPF with C# March 04, 2010
In this article we will see how we can change the Cell Alignment in DataGrid in WPF.
Reader Level:
 

Introduction

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

Creating WPF Application Project

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

CellAlignmentInDataGridFig1.gif

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

CellAlignmentInDataGridFig2.gif

The above class represents the structure of Employee Entity.

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

CellAlignmentInDataGridFig3.gif

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

CellAlignmentInDataGridFig4.gif

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.

CellAlignmentInDataGridFig5.gif

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

CellAlignmentInDataGridFig6.gif

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

CellAlignmentInDataGridFig7.gif

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

Hope this article helps.

Login to add your contents and source code to this article
share this article :
post comment
 

thank you very much. very helpful article. avm

Posted by avmfree avmfree Feb 24, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor