如何右对齐 DataGridView 列中的文本?

发布于 2024-10-21 05:07:23 字数 59 浏览 8 评论 0原文

如何右对齐 DataGridView 列中的文本?我正在编写一个 .NET WinForms 应用程序。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

Bonjour°[大白 2024-10-28 05:07:23
this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; 
this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; 
巡山小妖精 2024-10-28 05:07:23

要在 dataGridCell 中设置对齐文本,有两种方法:

为特定单元格设置对齐方式或为行的每个单元格设置对齐方式。

对于一列,请转到 Columns->DataGridViewCellStyle

对于每一列,请转到 RowDefaultCellStyle

控制面板如下所示:

在此处输入图像描述

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:

enter image description here

狼性发作 2024-10-28 05:07:23

我知道这已经很旧了,但是对于那些浏览这个问题的人来说,MUG4N 的答案将对齐使用相同默认单元格样式的所有列。我没有使用自动生成列,所以这是不可接受的。相反,我使用:

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

在这种情况下 e 来自:

Grd_ColumnAdded(object sender, DataGridViewColumnEventArgs e)  

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)  
情归归情 2024-10-28 05:07:23

您可以通过 Foreach 循环使用这个简单的代码一次编辑所有列

        foreach (DataGridViewColumn item in datagridview1.Columns)
        {
            item.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
        }

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;
        }
花之痕靓丽 2024-10-28 05:07:23
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" >
  <DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}">
       <Setter Property="HorizontalAlignment" Value="Right" />
    </Style>
  </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" >
  <DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}">
       <Setter Property="HorizontalAlignment" Value="Right" />
    </Style>
  </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
梦里人 2024-10-28 05:07:23

当我们处理对齐和配置时,通过使用 (C#) 匿名类型来减少重复任务可能很有用。更易于阅读,并且可以在一处编辑高级操作。就像这个 datagridview dg 配置一样

foreach (var conf in new [] { 
  new { Col = 0, WidthPercent = 40, Align = DataGridViewContentAlignment.MiddleCenter } ,
  new { Col = 1, WidthPercent = 30, Align = DataGridViewContentAlignment.MiddleCenter } ,
  new { Col = 2, WidthPercent = 30, Align = DataGridViewContentAlignment.MiddleCenter }
})
{
  dg.Columns[conf.Col].Width = (int)(dg.Width * conf.WidthPercent/100);
  dg.Columns[conf.Col].DefaultCellStyle.Alignment = conf.Align;
}

While we are dealing with alignment and configuration it can be useful to reduce repetitive tasks by using (C#) anonymous types. Much easier to read and advanced operations can be edited in one place. Like in this datagridview dg configuration

foreach (var conf in new [] { 
  new { Col = 0, WidthPercent = 40, Align = DataGridViewContentAlignment.MiddleCenter } ,
  new { Col = 1, WidthPercent = 30, Align = DataGridViewContentAlignment.MiddleCenter } ,
  new { Col = 2, WidthPercent = 30, Align = DataGridViewContentAlignment.MiddleCenter }
})
{
  dg.Columns[conf.Col].Width = (int)(dg.Width * conf.WidthPercent/100);
  dg.Columns[conf.Col].DefaultCellStyle.Alignment = conf.Align;
}
天气好吗我好吗 2024-10-28 05:07:23

DataGridViewColumn 列0 = dataGridViewGroup.Columns[0];
DataGridViewColumn 列1 = dataGridViewGroup.Columns[1];
column1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
列 1. 宽度 = 120;

DataGridViewColumn column0 = dataGridViewGroup.Columns[0];
DataGridViewColumn column1 = dataGridViewGroup.Columns[1];
column1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
column1.Width = 120;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文