format setting

Discussion in 'C#' started by maplecutie, May 25, 2016.

  1. maplecutie

    maplecutie New Member

    Joined:
    May 25, 2016
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Am a newbie at Dapfor .Net grid. Can someone tell me how I can do format setting.
     
  2. sobort84

    sobort84 New Member

    Joined:
    May 25, 2016
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0


    You are welcome to .Net. the easiest way you can set format is by following the code below.
    Code:
    	class Quote
    {
        ...
    
        [Format(typeof(CustomFormat))]
        public double Price
        {
            get { return _price; }
        }
    
        ...
    }
    
    
    CustomFormat : IFormat
    {
        private readonly string _formatString;
    
        public CustomFormat(string formatString)
        {
            _formatString = formatString;
        }
    
        public string Format(IDataField dataField)
        {
            return string.Format(CultureInfo.InvariantCulture, "{0:" + _formatString +"}", dataField.Value).Trim();
        }
    
        public bool CanParse(string text, IDataField dataField)
        {
            return false;
        }
    
        public void Parse(string text, IDataField dataField)
        {
        }
    }
    
    //An attribure returning an instance of IFormat object
    public class CustomFormatAttribute : FormatBaseAttribute
    {
        private readonly string _formatString;
    
        public CustomFormatAttribute(string formatString)
        {
     
    Last edited by a moderator: May 25, 2016
  3. Ami Desai

    Ami Desai Member

    Joined:
    Jan 5, 2017
    Messages:
    42
    Likes Received:
    17
    Trophy Points:
    8
    Location:
    Ahmedabad
    Home Page:
    http://www.ifourtechnolab.com/
    Hi,

    You can check this
    Code:
    class Quote
    {
        ...
    
        [Format(typeof(CustomFormat))]
        public double Price
        {
            get { return _price; }
        }
    
        ...
    }
    //Display a number using specific format string
    class CustomFormat : IFormat
    {
        private readonly string _formatString;
    
        public CustomFormat(string formatString)
        {
            _formatString = formatString;
        }
    
        public string Format(IDataField dataField)
        {
            return string.Format(CultureInfo.InvariantCulture, "{0:" + _formatString +"}", dataField.Value).Trim();
        }
    
        public bool CanParse(string text, IDataField dataField)
        {
            return false;
        }
    
        public void Parse(string text, IDataField dataField)
        {
        }
    }
    
    //An attribure returning an instance of IFormat object
    public class CustomFormatAttribute : FormatBaseAttribute
    {
        private readonly string _formatString;
    
        public CustomFormatAttribute(string formatString)
        {
            _formatString = formatString;
        }
    
        public override IFormat Format
        {
            get { return new CustomFormat(_formatString); }
        }
    }
    
    //Declare custom format
    class Quote
    {
        ...
    
        [CustomFormat("### ### ##0.##")]
        public double Price
        {
            get { return _price; }
        }
    
        ...
    }
    Thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice