Anoushka Seechurn
Anoushka Seechurn

Reputation: 2256

Centering Column and Row Text Content in DataGridView

I want to center the content of my Columns and Rows on DataGridView.

Is there a way of doing this by specifying the Column name, like:

dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; 

I want this to be applied to all the Columns of my DataGridView (which is completely coded programmatically).

How can I do this?

Upvotes: 8

Views: 25348

Answers (2)

NoWar
NoWar

Reputation: 37633

In case if you have got DataGridViewTextBoxColumn column in your DataGridView it might look like

var col1 = new DataGridViewTextBoxColumn()
{
   HeaderText = "Month",
   Width = 40,
   DefaultCellStyle = new DataGridViewCellStyle()
   {
      Alignment = DataGridViewContentAlignment.MiddleCenter,
      ForeColor = System.Drawing.Color.Black
   }
};

dataGridView.Columns.Add(col1);

Upvotes: 0

Priyank
Priyank

Reputation: 1384

Try this.

dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

Upvotes: 14

Related Questions