How can I right-align text in a DataGridView column?

前端 未结 5 1961
有刺的猬
有刺的猬 2021-02-01 12:06

How can I right-align text in a DataGridView column? I am writing a .NET WinForms application.

相关标签:
5条回答
  • 2021-02-01 12:22

    To set align text in dataGridCell you have two ways:

    Set the align for a specific cell or set for each cell of row.

    For one column go to Columns->DataGridViewCellStyle

    or

    For each column go to RowDefaultCellStyle

    The control panel is the same as the follow:

    0 讨论(0)
  • 2021-02-01 12:30
    this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; 
    
    0 讨论(0)
  • you can edit all the columns at once by using this simple code via Foreach loop

            foreach (DataGridViewColumn item in datagridview1.Columns)
            {
                item.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            }
    
    0 讨论(0)
  • 2021-02-01 12:35

    I know this is old, but for those surfing this question, the answer by MUG4N will align all columns that use the same defaultcellstyle. I'm not using autogeneratecolumns so that is not acceptable. Instead I used:

    e.Column.DefaultCellStyle = new DataGridViewCellStyle(e.Column.DefaultCellStyle);
    e.Column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
    

    In this case e is from:

    Grd_ColumnAdded(object sender, DataGridViewColumnEventArgs e)  
    
    0 讨论(0)
  • 2021-02-01 12:41
    <DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" >
      <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
           <Setter Property="HorizontalAlignment" Value="Right" />
        </Style>
      </DataGridTextColumn.ElementStyle>
    </DataGridTextColumn>
    
    0 讨论(0)
提交回复
热议问题