【问题标题】:DataGridView align one specific CellDataGridView 对齐一个特定的单元格
【发布时间】:2017-07-24 13:24:37
【问题描述】:

是否可以在 c# dataGridView 中对齐一个指定的单元格?

这个想法是这样的(但没有错误)

tT.Rows[j][1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight; 

【问题讨论】:

  • 对-1,无缘无故地这样做我认为是粗鲁的。
  • 您的目标是什么:Winforms、WPF、ASP..? 始终正确标记您的问题! - DataTables 只保存数据。没有格式化!您可能是指 DataGridView? - 在没有任何明显研究的情况下发布问题被认为是粗鲁的。但我同意:应该说明原因,至少对于第一次反对票,至少除非帖子绝对糟糕。这很糟糕,但并不绝对可怕。
  • 现在我有理由同意并进行了必要的编辑。我的意思是dataGridView。我会说混淆并不是直接粗鲁,但我们开始吧。

标签: c# winforms datagridview alignment cell


【解决方案1】:

是否可以在 DataGridView 中对齐单个指定单元格?

是的,这很容易。 选择Cell 并设置Style

DataGridViewCell dc = dataGridView1[0, 0];   // short reference
dc.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

或编码CellPainting事件有条件地对齐Cells

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
    {
        if (e.Value != null && e.Value.ToString() == "ddd")
        {
            e.CellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
        }
    }
}

当然,编写合适的条件取决于您。

请注意,重新加载行后,第一个选项的对齐方式将消失;第二种方式仍然会对齐那些符合条件的单元格..

【讨论】:

  • 非常感谢,我也很感激你甚至给出了两种可能的方法
猜你喜欢
  • 2019-03-07
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 2020-08-10
  • 2011-11-25
  • 1970-01-01
  • 2012-08-27
相关资源
最近更新 更多