menu

WinForms

  • Code Examples
  • Upgrade Guide
  • User Guide
  • Demos
  • Support
  • Forums
  • Download
Interface IDataGridFormatProvider - API Reference

    Show / Hide Table of Contents

    Interface IDataGridFormatProvider

    Provides a mechanism for retrieving the record and column of an object to control formatting.

    Inherited Members
    System.IFormatProvider.GetFormat(System.Type)
    System.ICustomFormatter.Format(System.String, System.Object, System.IFormatProvider)
    Namespace: Syncfusion.WinForms.DataGrid
    Assembly: Syncfusion.SfDataGrid.WinForms.dll
    Syntax
    public interface IDataGridFormatProvider : IFormatProvider, ICustomFormatter
    Examples

    The custom format provider which has derived from either IDataGridFormatProvider or IFormatProvider.

    // Sets the format provider which has derived from the IDataGridFormatProvider provider
    sfDataGrid1.Columns["OrderDate"].Format = new CustomFormatter();
    // Sets the format provider which has derived from the IFormatProvider and ICustomFormatter.
    sfDataGrid1.Columns["CustomerID"].Format = new Formatter();

    Derive the format provider from IDataGridFormatProvider to customize based on the record and column information. The below example shows how to write custom format provider which derives from IDataGridFormatProvider ,

    public class CustomFormatter : IDataGridFormatProvider
    {
        public object Format(string format, GridColumn gridColumn, object record, object value)
        {
            if ((format == null) || (value == null))
            {
                throw new ArgumentNullException((value == null) ? "value" : "format");
            }
            if (gridColumn.MappingName == "CustomerID" &&
                record is OrderInfo && (record as OrderInfo).ShipCountry == "Canada")
            {
                string val = value.ToString();
                val = "Can-" + val;
                return val;
            }
            return value.ToString();
        }
        public object GetFormat(Type formatType)
        {
            return this;
        }
    }

    The below example shows how to write custom format provider which derives from IFormatProvider ,

    public class CustomFormat : IFormatProvider, ICustomFormatter
    {
        public string Format(string format, object arg, IFormatProvider formatProvider
        {
            if (format == "C")
            {
                int value = Convert.ToInt16(arg);
                if (value > 100)
                    return "Maximum Value";
                else
                    return "Minimum Value";
            }
            return arg.ToString();
        }
        public object GetFormat(Type formatType)
        {
            return this;
        }
    }

    Methods

    Format(String, GridColumnBase, Object, Object)

    Gets the formatted display text with custom format.

    Declaration
    object Format(string format, GridColumnBase gridColumn, object record, object value)
    Parameters
    Type Name Description
    System.String format

    A format string.

    GridColumnBase gridColumn

    The column that needs to set the format.

    System.Object record

    The object of record.

    System.Object value

    The cell display text value.

    Returns
    Type Description
    System.Object

    Returns the formatted value.

    Back to top Generated by DocFX
    Copyright © 2001 - 2023 Syncfusion Inc. All Rights Reserved