Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3752606
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:16:02+00:00 2026-05-19T09:16:02+00:00

I’m following this question for formatting timespan value in the gridview. Format TimeSpan in

  • 0

I’m following this question for formatting timespan value in the gridview.

Format TimeSpan in DataGridView column

I’m diaplying only minutes value in grid as follows

DataGridViewColumn idleTimeColumn = myGridView.Columns["IdleTime"];
idleTimeColumn.DefaultCellStyle.FormatProvider = new TimeSpanFormatter();
idleTimeColumn.DefaultCellStyle.Format = "m";

This cell is editable, so when I enter 5 (that mean 5 min) in the cell, then it should take it minute, but it take it as day value (ie, 5.00:00:00).

I tried different values for format (mm,%m) but all these gives the same output.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:16:02+00:00Added an answer on May 19, 2026 at 9:16 am

    You have to implement the feature to parse an input value.

    TimeSpanFormatter can be used to convert a bound TimeSpan data to a formatted string for displaying. But it can’t convert an input value to TimeSpan. You have to convert an input value in DataGridView.CellParsing event handler.

    The following is the sample code to parse an input value.

    DataGridView.CellParsing event handler

    private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
    {
        if (e.DesiredType == typeof(TimeSpan))
        {
            TimeSpanParser parser = new TimeSpanParser();
            e.Value = parser.Parse(e.Value.ToString(), e.InheritedCellStyle.Format);
            e.ParsingApplied = true;
        }
    }
    

    TimeSpanParser class

    public class TimeSpanParser
    {
        private Regex formatParser;
    
        private List<TimeSpanPart> parts;
    
        public TimeSpanParser()
        {
            this.formatParser = new Regex("d{1,2}|h{1,2}|m{1,2}|s{1,2}|f{1,7}", RegexOptions.Compiled);
            this.parts = new List<TimeSpanPart>();
        }
    
        public TimeSpan Parse(string input, string format)
        {
            this.parts.Clear();
            var pattern = this.formatParser.Replace(format, m => ReplaceFormat(m));
            var match = Regex.Match(input, "^" + pattern + "$");
    
            TimeSpan result = new TimeSpan();
            for (int i = 1; i < match.Groups.Count; i++)
            {
                var value = Convert.ToDouble(match.Groups[i].Value);
                switch (this.parts[i - 1])
                {
                    case TimeSpanPart.Day:
                        result = result.Add(TimeSpan.FromDays(value));
                        break;
                    case TimeSpanPart.Hour:
                        result = result.Add(TimeSpan.FromHours(value));
                        break;
                    case TimeSpanPart.Minute:
                        result = result.Add(TimeSpan.FromMinutes(value));
                        break;
                    case TimeSpanPart.Second:
                        result = result.Add(TimeSpan.FromSeconds(value));
                        break;
                    case TimeSpanPart.Millisecond:
                        int digit = match.Groups[i].Value.Length;
                        value =value * Math.Pow(10, 3 - digit);
                        result = result.Add(TimeSpan.FromMilliseconds(value));
                        break;
                }
            }
    
            return result;
        }
    
        private string ReplaceFormat(Match match)
        {
            switch (match.Value)
            {
                case "dd":
                case "d":
                    this.parts.Add(TimeSpanPart.Day);
                    return "(\\d{1,2})";
                case "hh":
                case "h":
                    this.parts.Add(TimeSpanPart.Hour);
                    return "(\\d{1,2})";
                case "mm":
                case "m":
                    this.parts.Add(TimeSpanPart.Minute);
                    return "(\\d{1,2})";
                case "ss":
                case "s":
                    this.parts.Add(TimeSpanPart.Second);
                    return "(\\d{1,2})";
                case "fffffff":
                case "ffffff":
                case "fffff":
                case "ffff":
                case "fff":
                case "ff":
                case "f":
                    this.parts.Add(TimeSpanPart.Millisecond);
                    return "(\\d{1,7})";
                default:
                    return match.Value;
            }
        }
    }
    

    TimeSpanPart enum

    public enum TimeSpanPart
    {
        Day,
        Hour,
        Minute,
        Second,
        Millisecond,
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.