2016-11-06 76 views
1

在DevExpress GridView中是否可以將貨幣符號與左邊的值和右邊的值對齊? Here's an image details link貨幣符號左對齊並且GridView中的值正好對齊

+0

@Dmitry是,HTTPS://documentation.devexpress.com/#WindowsForms/DevExpressXtraGridColumnsGridColumn_DisplayFormattopic – SMW

+0

如果你能問這個問題在https://www.devexpress.com/Support/Center/Question/ ,因爲它會幫助你, – SMW

回答

0

此任務超出了常規文本格式。要完成它,您需要手動繪製單元格內容。

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系統

  • 有必要計算文本寬度,以確保價值和貨幣符號不會相互重疊。爲此,您可以使用通過事件參數可用的CalcTextSizeInt對象的方法。 DevExpress的使用自己的文字渲染引擎,所以標準Graphics.MeasureString方法不是在這種情況下有用

+0

感謝您的幫助,我只是需要它的顯示目的。 – nadzdaim