简体   繁体   English

列格式转换为货币

[英]Column format to currency in code behind

I want to set the p&l column I have to currency, it works well when I have it binded it's easy to set in XAML, but I have inner datagrids that don't use binding so I need to change its format to currency in code behind. 我想将我必须设置的p&l列设置为货币,将其绑定后可以很好地工作,在XAML中很容易设置,但是我内部的数据网格不使用绑定,因此我需要在后面的代码中将其格式更改为Currency 。 How do I do that, here's what I have so far: 我该怎么做,这是我到目前为止所拥有的:

private void dataGrid_RowDetailsVisibilityChanged(object sender, ExtendedGrid.Microsoft.Windows.Controls.DataGridRowDetailsEventArgs e)
{

        //Get TradeID  to query inner table by that trade id
        var row = e.Row;
        var drv = (DataRowView)row.DataContext;
        int TradeID = drv.Row.Field<int>(ColTradeID);

        ExtendedGrid.Microsoft.Windows.Controls.DataGrid innerDataGrid = e.DetailsElement as ExtendedGrid.Microsoft.Windows.Controls.DataGrid;

        DataTable TableView = null;

        //check if tradeid selected already exists
        if (selectedRowView.ContainsKey(TradeID))
        {
            TableView = selectedRowView[TradeID]; //selectedrowview is a dictionary of datatables that stores what to load in itemsource based on a tradeId
            innerDataGrid.ItemsSource = ((IListSource)selectedRowView[TradeID]).GetList();

            //Format Columns
            innerDataGrid.Columns[10].CellStyle = "C"; // **I want currency, this doesn't work, 
                                                      //I also tried typing defaultcelltype.format and nothing would come up its not available for use**
        }           
}

Also, I'm using the WPF Extended Datagrid not the WPF built in one. 另外,我使用的是WPF Extended Datagrid,而不是内置的WPF。

Check if there is a Binding Property on the Column. 检查列上是否有绑定属性。 If so set its String.Format. 如果是这样,请设置其String.Format。 I haven't worked with Extended Datagrid but this solution has worked in the past for me on other C# WPF grids. 我没有使用Extended Datagrid,但是该解决方案过去在其他C#WPF网格上对我有用。

Got it, here's how I did it: 知道了,这是我的做法:

innerDataGrid.Columns[10].ClipboardContentBinding.StringFormat = "$00.00";

Thanks to @Vulcronos 感谢@Vulcronos

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM