wpfinfragisticsxamdatagrid

xamDataGrid formatting numeric fields to 4 decimal places


I have a WPF project that contains a form with an Infragistics xamDataGrid on it. The data that is displayed in this xamDataGrid changes depending on the caller.

What I'm trying to do is to format a numeric column (Rate) to display 4 decimal places. I've found several sites with instructions for how to do this with XAML, but since the data in this grid is dynamic, I need to do this in the code-behind. (Preferably C#, but I can manage with VB.NET.)

The caller currently passes me the data to display in the grid as well as which columns to hide and which columns are editable. So I can have it pass me a list of columns to format and the format string to use.

I just need to figure out how to tell the xamDataGrid to format a column or a field in a certain way.

For those of you who want to see the XAML, here it is for the datagrid:

<igWPF:XamDataGrid x:Name="GrdMaint"
                   Margin="10"
                   DataSource="{Binding Source={StaticResource cvsDataGrid}}"
                   BorderBrush="Black"
                   BorderThickness="1"
                   HorizontalAlignment="Stretch"
                   VerticalAlignment="Stretch" 
                   IsSynchronizedWithCurrentItem="True"
                   Theme="Office2k7Blue" 
                   GroupByAreaLocation="None" 
                   FieldLayoutInitialized="GrdMaint_OnFieldLayoutInitialized"
                   PreviewMouseDoubleClick="GrdMaint_OnPreviewMouseDoubleClick"
                   RecordUpdated="GrdMaint_RecordUpdated">

    <igWPF:XamDataGrid.FieldSettings>
        <igWPF:FieldSettings AllowRecordFiltering="True" 
                             FilterLabelIconDropDownType="MultiSelectExcelStyle" 
                             Width="Auto" />
    </igWPF:XamDataGrid.FieldSettings>

    <igWPF:XamDataGrid.FieldLayoutSettings>
        <igWPF:FieldLayoutSettings HighlightAlternateRecords="True" 
                                   FilterUIType="LabelIcons" 
                                   AllowDelete ="False" 
                                   AutoGenerateFields="True" />
    </igWPF:XamDataGrid.FieldLayoutSettings>
</igWPF:XamDataGrid>

Solution

  • Okay, after fighting with the grid and searching for a couple days, I stumbled across the magical search string that found an answer for me. I found this post on the Infragistics support forums (StackOverflow has spoiled me. It's so much nicer, here.) And that enabled me to come up with this solution. In the OnFieldLayoutInitialized event, I can iterate through each of the fields and determine whether or not I want to format that field. In my case, I'm checking to see if the field name is in a dictionary of field names/format strings. If so, I apply the format string.

    private void GrdMaint_OnFieldLayoutInitialized(object sender, FieldLayoutInitializedEventArgs e)
    {
        Dictionary<string,string> NumericColumnsToFormat = _Vm.GetNumericColumnFormats(_currentStep);
    
        foreach (var fld in e.FieldLayout.Fields)
        {
            // apply custom numeric format to specific field, if necessary.
            if (NumericColumnsToFormat.Keys.Contains(fld.Name))
            {
                string formatString = NumericColumnsToFormat[fld.Name];
                if (!string.IsNullOrEmpty(formatString))
                {
                    var numberEditorStyle = new Style(typeof(XamNumericEditor));
                    numberEditorStyle.Setters.Add(new Setter(XamNumericEditor.FormatProperty, formatString));
                    fld.Settings.EditorStyle = numberEditorStyle;
                }
            }
        }
    }