简体   繁体   中英

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. 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.

Check if there is a Binding Property on the Column. If so set its String.Format. I haven't worked with Extended Datagrid but this solution has worked in the past for me on other C# WPF grids.

Got it, here's how I did it:

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

Thanks to @Vulcronos

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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