【问题标题】:Currency symbol Align left and values align right in GridView货币符号在 GridView 中左对齐,值右对齐
【发布时间】:2016-11-06 12:56:23
【问题描述】:

是否可以在 DevExpress GridView 中将货币符号左对齐,值右对齐?

【问题讨论】:

标签: c# winforms devexpress


【解决方案1】:

此任务超出了常规文本格式。为此,您需要手动绘制单元格内容。

XtraGrid 为此提供了事件:CustomDrawCellevent argument object 提供对图形对象、单元格边界和手动绘制单元格文本所需的其他信息的引用。

private void OnGridViewCustomDrawCell(object sender, RowCellCustomDrawEventArgs e) {
    switch (e.Column.FieldName) {
        case "Debit":
            DrawDebitCell(e);
            break;  
    }       
}

private void DrawDebitCell(RowCellCustomDrawEventArgs e) {
    e.Handled = true;
    string text = string.Format(CultureInfo.CurrentCulture, "{0} {1:n2}", CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol, e.CellValue);
    Size textSize = e.Appearance.CalcTextSizeInt(e.Cache, text, int.MaxValue);
    e.Appearance.DrawBackground(e.Cache, e.Bounds);
    if (Convert.ToInt32(textSize.Width) > e.Bounds.Width)
        e.Appearance.DrawString(e.Cache, text, e.Bounds);
    else {
        StringFormat stringFormat = e.Appearance.GetStringFormat();
        string valueText = string.Format(CultureInfo.CurrentCulture, "{0:n2}", e.CellValue);
        stringFormat.Alignment = StringAlignment.Near;
        e.Appearance.DrawString(e.Cache, CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol, e.Bounds, e.Appearance.Font, stringFormat);
        stringFormat.Alignment = StringAlignment.Far;
        e.Appearance.DrawString(e.Cache, valueText, e.Bounds, e.Appearance.Font, stringFormat);
    }
}

这种方法有一些缺点:

  • 内置Export and Printing系统不使用手动绘制的单元格内容

  • 需要计算文本宽度以确保值和货币符号不会相互重叠。为此,您可以使用可通过事件参数获得的 AppearanceObject 对象的CalcTextSizeInt 方法。 DevExpress 使用自己的文本渲染引擎,因此标准的Graphics.MeasureString 方法在这种情况下没有用处

【讨论】:

  • 感谢您的帮助,我只需要它用于显示目的。
猜你喜欢
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 2012-03-27
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
  • 2021-02-21
相关资源
最近更新 更多