how to align the cells in a datagridview to the right in c#.net?
I need to align some of the columns in a datagridview to the right. But I can't find any property to control the alignment. How can i accomplish that?
I'm programming in c#.net (visual studio 2005).
Status:
Open Apr 04, 2007 - 11:28 AM
.net, C#, information technology, datagridview
4answers
Answers
Apr 04, 2007 - 12:59 PM
Either select your DataGridView in thedesigner window and set the property "RowStyle|HorizontalAlign" to "Right" or change the HTML code and add "align=right" to the tags.
Cheers
Peter
Apr 04, 2007 - 01:25 PM
thanks, Peter.
I forgot to say that it's windows forms, not asp.net, so the solution doesn't work unfortunately.
Apr 04, 2007 - 01:36 PM
Oh sorry, I am deep in ASP.Net development at the moment so my brain was focussed on this! Sorry!
For a windows app, look at RowTemplate property of the GridView component, expand the sub nodes and click on the [...] for DefaultCellStyle. This will open the CellStyle Builder. Set the Alignment property in the Layout section.
Cheers
Peter
Apr 05, 2007 - 02:00 AM
thank you Peter, that worked.
I found that the equivalent by code is:
this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
and the points go to..... Peter!!! :)
Answer this question
Share Your Own Experience & Expertise
We look to ensure that every question is answered by the best people with relevant expertise and experience, the best answers include multiple perspectives. Do you have relevant expertise or experience to contribute your answer to any of these commonly asked questions?
Add New Comment