winforms - gridview单元格时间跨度编辑问题

时间:2011-01-04 06:02:08

标签: winforms datagridview formatting

我正在关注这个问题,以便在gridview中格式化时间跨度值。

Format TimeSpan in DataGridView column

我在网格中只显示了几分钟的值,如下所示

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

这个单元格是可编辑的,所以当我在单元格中输入5(表示5分钟)时,它应该花费一分钟,但它将它作为日期值(即5.00:00:00)。

我为格式(mm,%m)尝试了不同的值,但所有这些都给出了相同的输出。

3 个答案:

答案 0 :(得分:2)

您必须实现该功能才能解析输入值。

TimeSpanFormatter可用于将绑定的TimeSpan数据转换为格式化字符串以进行显示。但它无法将输入值转换为TimeSpan。您必须在DataGridView.CellParsing事件处理程序中转换输入值。

以下是解析输入值的示例代码。

DataGridView.CellParsing事件处理程序

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类

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枚举

public enum TimeSpanPart
{
    Day,
    Hour,
    Minute,
    Second,
    Millisecond,
}

答案 1 :(得分:0)

我相信您正在正确处理单元格格式化事件

Youe were referring this i guess

答案 2 :(得分:-2)

尝试mm%m代替m; idleTimeColumn.DefaultCellStyle.Format = "mm";