【发布时间】:2016-11-06 12:56:23
【问题描述】:
是否可以在 DevExpress GridView 中将货币符号左对齐,值右对齐?
【问题讨论】:
-
如果你可以在devexpress.com/Support/Center/Question问这个问题,因为它会对你有所帮助,
标签: c# winforms devexpress
是否可以在 DevExpress GridView 中将货币符号左对齐,值右对齐?
【问题讨论】:
标签: c# winforms devexpress
此任务超出了常规文本格式。为此,您需要手动绘制单元格内容。
XtraGrid 为此提供了事件:CustomDrawCell。 event 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 方法在这种情况下没有用处
【讨论】: