DevExpress GridViewで通貨記号を左に、値を右に揃えることは可能ですか?
このタスクは、通常のテキスト形式を超えています。これを行うには、セルの内容を手動で描画する必要があります。
XtraGridは、この目的のためにイベントを提供します:CustomDrawCell。イベント引数オブジェクトは、手動でのセルテキストを描画するために必要なグラフィックオブジェクト、セル境界、および他の情報への参照を提供します。
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);
}
}
このアプローチにはいくつかの欠点があります。
手動で描画されたセルコンテンツは、組み込みのエクスポートおよび印刷システムでは使用されません。
値と通貨記号が重ならないように、テキストの幅を計算する必要があります。この目的のために、イベント引数を介して使用可能なAppearanceObjectオブジェクトのCalcTextSizeIntメソッドを使用できます。DevExpressは独自のテキストレンダリングエンジンを使用するため、この場合、標準のGraphics.MeasureStringメソッドは役に立ちません。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加